#include<cstdio> #include<algorithm> #include<iostream> #define S 200007 using namespace std; typedef long long ll; int T[S]; int P[5][5]; ll mod = 1000000007; ll pot(ll x, ll y){ if(y == 0) return 1; if(y == 1) return x; if(y % 2 == 0){ ll p = pot(x,y/2); return (p*p)%mod; }else{ return (pot(x,y-1) * x)%mod; } } ll odw = pot(3,mod-2)%mod; int fC(int x){ int p = x%6; if(p < 0) p += 6; if(p == 0) return 2; else if(p == 1) return 1; else if(p == 2) return -1; else if(p == 3) return -2; else if(p == 4) return -1; else if(p == 5) return 1; return 0; } int n; ll P2[S]; int reds = 0; int greens = 0; int blues = 0; ll f(){ if(n == 1){ if(T[1] == 2) return 2; else return 1; } ll bads = 0; if(n%2 == 1){ if(P[0][0] == 0 && P[1][1] == 0) bads++; if(P[0][1] == 0 && P[1][0] == 0) bads++; } int p = 0; if(n % 3 == 0){ if(reds % 3 == 0) p = 0; else if(reds % 3 == 1) p = 2; else p = 1; //reds i greens przystaja do 0 }else if (n % 3 == 1){ //reds i greens przystaja do 2 if(reds % 3 == 0) p = 2; else if(reds % 3 == 1) p = 1; else p = 0; }else{ //reds i greens przystaja do 1 if(reds % 3 == 0) p = 1; else if(reds % 3 == 1) p = 0; else p = 2; } ll p2 = P2[blues]; int x = blues; if(p == 1) x -= 2; else if(p == 2) x -= 4; p2 += fC(x); p2 *= odw; p2 %= mod; bads += p2; ll goods = P2[blues]; goods -= bads; goods %= mod; if(goods < 0) goods += mod; return goods; } int main(void){ P2[0] = 1; for(int i = 1; i < S;i++){ P2[i] = P2[i-1] * (ll)2; P2[i] %= mod; } int q; scanf("%d %d",&n,&q); string s; cin >> s; for(int i = 1; i <= n;i++){ if(s[i-1] == 'N'){ T[i] = 2; blues++; }else if(s[i-1] == 'C'){ T[i] = 0; reds++; }else{ T[i] = 1; greens++; } P[T[i]][i%2]++; } printf("%lld\n",f()); for(int i = 1; i <= q;i++){ int x; char c; scanf("%d %c",&x,&c); if(T[x] == 2){ blues--; }else if(T[x] == 0){ reds--; }else{ greens--; } P[T[x]][x%2]--; if(c == 'N'){ T[x] = 2; blues++; }else if(c == 'C'){ T[x] = 0; reds++; }else{ T[x] = 1; greens++; } P[T[x]][x%2]++; printf("%lld\n",f()); } return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | #include<cstdio> #include<algorithm> #include<iostream> #define S 200007 using namespace std; typedef long long ll; int T[S]; int P[5][5]; ll mod = 1000000007; ll pot(ll x, ll y){ if(y == 0) return 1; if(y == 1) return x; if(y % 2 == 0){ ll p = pot(x,y/2); return (p*p)%mod; }else{ return (pot(x,y-1) * x)%mod; } } ll odw = pot(3,mod-2)%mod; int fC(int x){ int p = x%6; if(p < 0) p += 6; if(p == 0) return 2; else if(p == 1) return 1; else if(p == 2) return -1; else if(p == 3) return -2; else if(p == 4) return -1; else if(p == 5) return 1; return 0; } int n; ll P2[S]; int reds = 0; int greens = 0; int blues = 0; ll f(){ if(n == 1){ if(T[1] == 2) return 2; else return 1; } ll bads = 0; if(n%2 == 1){ if(P[0][0] == 0 && P[1][1] == 0) bads++; if(P[0][1] == 0 && P[1][0] == 0) bads++; } int p = 0; if(n % 3 == 0){ if(reds % 3 == 0) p = 0; else if(reds % 3 == 1) p = 2; else p = 1; //reds i greens przystaja do 0 }else if (n % 3 == 1){ //reds i greens przystaja do 2 if(reds % 3 == 0) p = 2; else if(reds % 3 == 1) p = 1; else p = 0; }else{ //reds i greens przystaja do 1 if(reds % 3 == 0) p = 1; else if(reds % 3 == 1) p = 0; else p = 2; } ll p2 = P2[blues]; int x = blues; if(p == 1) x -= 2; else if(p == 2) x -= 4; p2 += fC(x); p2 *= odw; p2 %= mod; bads += p2; ll goods = P2[blues]; goods -= bads; goods %= mod; if(goods < 0) goods += mod; return goods; } int main(void){ P2[0] = 1; for(int i = 1; i < S;i++){ P2[i] = P2[i-1] * (ll)2; P2[i] %= mod; } int q; scanf("%d %d",&n,&q); string s; cin >> s; for(int i = 1; i <= n;i++){ if(s[i-1] == 'N'){ T[i] = 2; blues++; }else if(s[i-1] == 'C'){ T[i] = 0; reds++; }else{ T[i] = 1; greens++; } P[T[i]][i%2]++; } printf("%lld\n",f()); for(int i = 1; i <= q;i++){ int x; char c; scanf("%d %c",&x,&c); if(T[x] == 2){ blues--; }else if(T[x] == 0){ reds--; }else{ greens--; } P[T[x]][x%2]--; if(c == 'N'){ T[x] = 2; blues++; }else if(c == 'C'){ T[x] = 0; reds++; }else{ T[x] = 1; greens++; } P[T[x]][x%2]++; printf("%lld\n",f()); } return 0; } |