#include <bits/stdc++.h> using namespace std; const int N = 200001; const long long M = 1e9 + 7; int n, q, cc[2], zz[2]; long long t[N][3], p[N], nn; string s; char ch; long long zle() { if (n == 1 || n % 2 == 0) { return 0; } int r = 0; if (cc[0] == 0 && zz[1] == 0) { r++; } if (cc[1] == 0 && zz[0] == 0) { r++; } return r; } void add(int i, char ch) { if (ch == 'N') { nn++; } if (ch == 'C') { cc[i % 2]++; } if (ch == 'Z') { zz[i % 2]++; } } void rem(int i, char ch) { if (ch == 'N') { nn--; } if (ch == 'C') { cc[i % 2]--; } if (ch == 'Z') { zz[i % 2]--; } } long long res() { int mod = 3; mod += (cc[0] + cc[1]) % 3; mod += nn % 3; mod -= (zz[0] + zz[1]) % 3; mod %= 3; if (mod % 2) {mod += 3;} return (2 * M + p[nn] - t[nn][mod / 2] - zle()) % M; } int main() { ios_base::sync_with_stdio(0); cin >> n >> q >> s; for (int i = 0; i < s.size(); i++) { add(i, s[i]); } t[0][0] = 1; p[0] = 1; for (int i = 1; i <= n; i++) { for (int j = 0; j < 3; j++) { t[i][j] = t[i - 1][j] + t[i - 1][(j + 2) % 3]; t[i][j] %= M; } p[i] = 2 * p[i - 1]; p[i] %= M; } cout << res() << endl; int i; while (q--) { cin >> i >> ch; i--; rem(i, s[i]); add(i, ch); s[i] = ch; cout << res() << endl; } 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 | #include <bits/stdc++.h> using namespace std; const int N = 200001; const long long M = 1e9 + 7; int n, q, cc[2], zz[2]; long long t[N][3], p[N], nn; string s; char ch; long long zle() { if (n == 1 || n % 2 == 0) { return 0; } int r = 0; if (cc[0] == 0 && zz[1] == 0) { r++; } if (cc[1] == 0 && zz[0] == 0) { r++; } return r; } void add(int i, char ch) { if (ch == 'N') { nn++; } if (ch == 'C') { cc[i % 2]++; } if (ch == 'Z') { zz[i % 2]++; } } void rem(int i, char ch) { if (ch == 'N') { nn--; } if (ch == 'C') { cc[i % 2]--; } if (ch == 'Z') { zz[i % 2]--; } } long long res() { int mod = 3; mod += (cc[0] + cc[1]) % 3; mod += nn % 3; mod -= (zz[0] + zz[1]) % 3; mod %= 3; if (mod % 2) {mod += 3;} return (2 * M + p[nn] - t[nn][mod / 2] - zle()) % M; } int main() { ios_base::sync_with_stdio(0); cin >> n >> q >> s; for (int i = 0; i < s.size(); i++) { add(i, s[i]); } t[0][0] = 1; p[0] = 1; for (int i = 1; i <= n; i++) { for (int j = 0; j < 3; j++) { t[i][j] = t[i - 1][j] + t[i - 1][(j + 2) % 3]; t[i][j] %= M; } p[i] = 2 * p[i - 1]; p[i] %= M; } cout << res() << endl; int i; while (q--) { cin >> i >> ch; i--; rem(i, s[i]); add(i, ch); s[i] = ch; cout << res() << endl; } return 0; } |