#include <bits/stdc++.h> using namespace std; using ll = long long; #define int ll const int MAX_N = 2e5+2; const int M = 1e9+7; int tab[3][3][MAX_N]; int two[MAX_N]; const int inv_3 = 333333336; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int pow = 1; for (int k = 0; k < MAX_N; k++) { int neg = 1; if (k % 2) neg = -1; tab[0][0][k] = (2*neg + pow) * inv_3; tab[0][0][k] %= M; tab[0][1][k] = (-neg + pow) * inv_3; tab[0][1][k] %= M; tab[0][2][k] = tab[0][1][k]; tab[1][0][k] = 2*(pow - neg) * inv_3; tab[1][0][k] %= M; tab[1][1][k] = (neg + (pow*2)%M) * inv_3; tab[1][1][k] %= M; tab[1][2][k] = (neg + (pow*2)%M) * inv_3; tab[1][2][k] %= M; tab[2][0][k] = 2*(neg + (pow*2)%M) * inv_3; tab[2][0][k] %= M; tab[2][1][k] = (-neg + (pow*4)%M) * inv_3; tab[2][1][k] %= M; tab[2][2][k] = (-neg + (pow*4)%M) * inv_3; tab[2][2][k] %= M; pow *= 8; pow %= M; } two[0] = 1; for (int k = 1; k < MAX_N; k++) { two[k] = two[k-1] * 2; two[k] %= M; } int N, q; cin >> N >> q; string s; cin >> s; int cev = 0, cod = 0, zev = 0, zod = 0, n = 0; for (int i = 0; i < N; i++) { if (s[i] == 'C') { if (i % 2 == 0) cev++; else cod++; } else if (s[i] == 'Z') { if (i % 2 == 0) zev++; else zod++; } else { n++; } } do { int c = cod + cev, z = zod + zev; int d = abs(c-z)%3; int bad = 0; int n_ = n; bad = tab[n_%3][d%3][n_/3]; if (N % 2 != 0 && N > 1) { if (cev == 0 && zod == 0) bad++; if (cod == 0 && zev == 0) bad++; } cout << (two[n] - bad+M)%M << '\n'; if (q) { int i; char c; cin >> i >> c; i--; if (c == 'C') { if (i % 2 == 0) cev++; else cod++; } else if (c == 'Z') { if (i % 2 == 0) zev++; else zod++; } else { n++; } if (s[i] == 'C') { if (i % 2 == 0) cev--; else cod--; } else if (s[i] == 'Z') { if (i % 2 == 0) zev--; else zod--; } else { n--; } s[i] = c; } } while (q--); 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 | #include <bits/stdc++.h> using namespace std; using ll = long long; #define int ll const int MAX_N = 2e5+2; const int M = 1e9+7; int tab[3][3][MAX_N]; int two[MAX_N]; const int inv_3 = 333333336; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int pow = 1; for (int k = 0; k < MAX_N; k++) { int neg = 1; if (k % 2) neg = -1; tab[0][0][k] = (2*neg + pow) * inv_3; tab[0][0][k] %= M; tab[0][1][k] = (-neg + pow) * inv_3; tab[0][1][k] %= M; tab[0][2][k] = tab[0][1][k]; tab[1][0][k] = 2*(pow - neg) * inv_3; tab[1][0][k] %= M; tab[1][1][k] = (neg + (pow*2)%M) * inv_3; tab[1][1][k] %= M; tab[1][2][k] = (neg + (pow*2)%M) * inv_3; tab[1][2][k] %= M; tab[2][0][k] = 2*(neg + (pow*2)%M) * inv_3; tab[2][0][k] %= M; tab[2][1][k] = (-neg + (pow*4)%M) * inv_3; tab[2][1][k] %= M; tab[2][2][k] = (-neg + (pow*4)%M) * inv_3; tab[2][2][k] %= M; pow *= 8; pow %= M; } two[0] = 1; for (int k = 1; k < MAX_N; k++) { two[k] = two[k-1] * 2; two[k] %= M; } int N, q; cin >> N >> q; string s; cin >> s; int cev = 0, cod = 0, zev = 0, zod = 0, n = 0; for (int i = 0; i < N; i++) { if (s[i] == 'C') { if (i % 2 == 0) cev++; else cod++; } else if (s[i] == 'Z') { if (i % 2 == 0) zev++; else zod++; } else { n++; } } do { int c = cod + cev, z = zod + zev; int d = abs(c-z)%3; int bad = 0; int n_ = n; bad = tab[n_%3][d%3][n_/3]; if (N % 2 != 0 && N > 1) { if (cev == 0 && zod == 0) bad++; if (cod == 0 && zev == 0) bad++; } cout << (two[n] - bad+M)%M << '\n'; if (q) { int i; char c; cin >> i >> c; i--; if (c == 'C') { if (i % 2 == 0) cev++; else cod++; } else if (c == 'Z') { if (i % 2 == 0) zev++; else zod++; } else { n++; } if (s[i] == 'C') { if (i % 2 == 0) cev--; else cod--; } else if (s[i] == 'Z') { if (i % 2 == 0) zev--; else zod--; } else { n--; } s[i] = c; } } while (q--); return 0; } |