#include<bits/stdc++.h> using namespace std; constexpr int mod = 1000000007; constexpr int czer = 0; constexpr int ziel = 1; constexpr int nieb = 2; int color_of(char x) { if (x == 'C') return czer; if (x == 'Z') return ziel; return nieb; } long long power(int a, int e) { if (e == 0) return 1; if (e % 2 == 1) return (a * power(a, e - 1)) % mod; long long t = power(a, e / 2); return t * t % mod; } // 3^{-1} modulo 1000000007 constexpr int inv_3 = 333333336; int tab[6][3] = { {1, 0, 0}, // 1 0 0 {1, 1, 0}, // 1 1 0 {0, 1, 0}, // 1 2 1 {0, 1, 1}, // 2 3 3 {0, 0, 1}, // 5 5 6 {1, 0, 1}, // 11 10 11 }; long long sum_3(int n, int r) { int pow_2 = power(2, n); if (n % 2 == 0) pow_2 = (pow_2 + mod - 1) % mod; else pow_2 = (pow_2 + mod - 2) % mod; return (((long long)(pow_2) * inv_3 + tab[n % 6][r])) % mod; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, q; cin >> n >> q; string s; cin >> s; vector<int> in(n); int cnt[3] {0, 0, 0}; for (int i = 0; i < n; i++) { in[i] = color_of(s[i]); cnt[in[i]]++; } for (int query = 0; query <= q; query++) { if (query >= 1) { int id; char ch; cin >> id >> ch; int old_col = in[id - 1]; int new_col = color_of(ch); cnt[old_col]--; cnt[new_col]++; in[id - 1] = new_col; } if (cnt[nieb] == 0) { int ans((cnt[ziel] % 3) != (cnt[czer] % 3)); for (int d : {0, 1}) { bool good = (n >= 3); for (int i = 0; i < n; i++) good &= (((in[i] + i) % 2) == d); ans -= int(good); } cout << ans << "\n"; continue; } // initially, assume all are good int ans = power(2, cnt[nieb]); if (n >= 3 and n % 2 == 1 and cnt[nieb] > 0) { for (int d : {0, 1}) { bool good = true; for (int i = 0; i < n; i++) good &= (in[i] == nieb or ((in[i] + i) % 2) == d); if (good) ans = (ans + mod - 1) % mod; } } int dif = (3 + cnt[czer] - (cnt[ziel] % 3)) % 3; int r = (3 + 2 * dif - (cnt[nieb] % 3)) % 3; cout << (mod + ans - sum_3(cnt[nieb], r)) % mod << "\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 | #include<bits/stdc++.h> using namespace std; constexpr int mod = 1000000007; constexpr int czer = 0; constexpr int ziel = 1; constexpr int nieb = 2; int color_of(char x) { if (x == 'C') return czer; if (x == 'Z') return ziel; return nieb; } long long power(int a, int e) { if (e == 0) return 1; if (e % 2 == 1) return (a * power(a, e - 1)) % mod; long long t = power(a, e / 2); return t * t % mod; } // 3^{-1} modulo 1000000007 constexpr int inv_3 = 333333336; int tab[6][3] = { {1, 0, 0}, // 1 0 0 {1, 1, 0}, // 1 1 0 {0, 1, 0}, // 1 2 1 {0, 1, 1}, // 2 3 3 {0, 0, 1}, // 5 5 6 {1, 0, 1}, // 11 10 11 }; long long sum_3(int n, int r) { int pow_2 = power(2, n); if (n % 2 == 0) pow_2 = (pow_2 + mod - 1) % mod; else pow_2 = (pow_2 + mod - 2) % mod; return (((long long)(pow_2) * inv_3 + tab[n % 6][r])) % mod; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, q; cin >> n >> q; string s; cin >> s; vector<int> in(n); int cnt[3] {0, 0, 0}; for (int i = 0; i < n; i++) { in[i] = color_of(s[i]); cnt[in[i]]++; } for (int query = 0; query <= q; query++) { if (query >= 1) { int id; char ch; cin >> id >> ch; int old_col = in[id - 1]; int new_col = color_of(ch); cnt[old_col]--; cnt[new_col]++; in[id - 1] = new_col; } if (cnt[nieb] == 0) { int ans((cnt[ziel] % 3) != (cnt[czer] % 3)); for (int d : {0, 1}) { bool good = (n >= 3); for (int i = 0; i < n; i++) good &= (((in[i] + i) % 2) == d); ans -= int(good); } cout << ans << "\n"; continue; } // initially, assume all are good int ans = power(2, cnt[nieb]); if (n >= 3 and n % 2 == 1 and cnt[nieb] > 0) { for (int d : {0, 1}) { bool good = true; for (int i = 0; i < n; i++) good &= (in[i] == nieb or ((in[i] + i) % 2) == d); if (good) ans = (ans + mod - 1) % mod; } } int dif = (3 + cnt[czer] - (cnt[ziel] % 3)) % 3; int r = (3 + 2 * dif - (cnt[nieb] % 3)) % 3; cout << (mod + ans - sum_3(cnt[nieb], r)) % mod << "\n"; } } |