#include <iostream> using namespace std; long long pref0[200007]; long long pref12[200007]; long long pw2[200007]; long long m = (long long) 1000000007; long long mod(long long num){ num -= num/m*m; if(num<0) num += m; return num; } string s; int r, g, b, n, q; int rp, rn, gp, gn; long long solve(){ long long result = 0LL; if((r+2*g)%3){ result = pref12[b]; } else{ result = pref0[b]; } if(n>1){ if((n&1) && (!rp && !gn)){ result--; } if((n&1) && (!rn && !gp)){ result--; } } return mod(result); } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); pw2[0] = 1; pref0[0] = 0; pref12[0] = 1; for(int i=1; i<=200000; i++){ pw2[i] = mod(pw2[i-1]*2); pref0[i] = mod(pw2[i] - pref0[i-1]); pref12[i] = mod(pw2[i] - pref12[i-1]); } cin >> n >> q; cin >> s; for(int i=0; i<n; i++){ if(s[i]=='C'){ r++; if(i&1) rn++; else rp++; } if(s[i]=='Z'){ g++; if(i&1) gn++; else gp++; } if(s[i]=='N') b++; } int k; char kk; cout << solve() << '\n'; for(int i=0; i<q; i++){ cin >> k >> kk; k--; if(s[k] == 'C'){ if(k&1) rn--; else rp--; r--; } if(s[k] == 'Z'){ if(k&1) gn--; else gp--; g--; } if(s[k] == 'N') b--; s[k] = kk; if(kk == 'C'){ if(k&1) rn++; else rp++; r++; } if(kk == 'Z'){ if(k&1) gn++; else gp++; g++; } if(kk == 'N') b++; cout << solve() << '\n'; } }
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 | #include <iostream> using namespace std; long long pref0[200007]; long long pref12[200007]; long long pw2[200007]; long long m = (long long) 1000000007; long long mod(long long num){ num -= num/m*m; if(num<0) num += m; return num; } string s; int r, g, b, n, q; int rp, rn, gp, gn; long long solve(){ long long result = 0LL; if((r+2*g)%3){ result = pref12[b]; } else{ result = pref0[b]; } if(n>1){ if((n&1) && (!rp && !gn)){ result--; } if((n&1) && (!rn && !gp)){ result--; } } return mod(result); } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); pw2[0] = 1; pref0[0] = 0; pref12[0] = 1; for(int i=1; i<=200000; i++){ pw2[i] = mod(pw2[i-1]*2); pref0[i] = mod(pw2[i] - pref0[i-1]); pref12[i] = mod(pw2[i] - pref12[i-1]); } cin >> n >> q; cin >> s; for(int i=0; i<n; i++){ if(s[i]=='C'){ r++; if(i&1) rn++; else rp++; } if(s[i]=='Z'){ g++; if(i&1) gn++; else gp++; } if(s[i]=='N') b++; } int k; char kk; cout << solve() << '\n'; for(int i=0; i<q; i++){ cin >> k >> kk; k--; if(s[k] == 'C'){ if(k&1) rn--; else rp--; r--; } if(s[k] == 'Z'){ if(k&1) gn--; else gp--; g--; } if(s[k] == 'N') b--; s[k] = kk; if(kk == 'C'){ if(k&1) rn++; else rp++; r++; } if(kk == 'Z'){ if(k&1) gn++; else gp++; g++; } if(kk == 'N') b++; cout << solve() << '\n'; } } |