#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 50; const int mod = 1e9 + 7; inline int inc(int x, int y) { x += y - mod; return x + (x >> 31 & mod); } inline int dec(int x, int y) { x -= y; return x + (x >> 31 & mod); } inline int mul(int x, int y) { return 1ll * x * y % mod; } auto inc(const auto &first, const auto ...args) { return inc(first, inc(args...)); } auto mul(const auto &first, const auto ...args) { return mul(first, mul(args...)); } inline void upd(int &x, int y) { x = inc(x, y); } inline void dpu(int &x, int y) { x = dec(x, y); } inline void pud(int &x, int y) { x = mul(x, y); } int n, q, su, s[N][3], x, y, cnt; string st; void add(int p, int v) { switch (st[p]) { case 'C': su -= v; (p % 2 ? x : y) += v; break; case 'Z': su += v; (p % 2 ? y : x) += v; break; case 'N': su -= v; cnt += v; x += v; y += v; break; default: throw; } } int64_t solve() { int o = (su % 3 + 3) % 3; int ways = 0; for (int i : {0, 1, 2}) if (i != o) upd(ways, s[cnt][i]); if (n == 1) return ways; if (n % 2 && x == n) --ways; if (n % 2 && y == n) --ways; return (ways % mod + mod) % mod; }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin >> n >> q; s[0][0] = 1; for (int i = 1; i <= n; ++i) { s[i][0] = inc(s[i - 1][0], s[i - 1][2]); s[i][1] = inc(s[i - 1][0], s[i - 1][1]); s[i][2] = inc(s[i - 1][1], s[i - 1][2]); } cin >> st; for (int i = 0; i < n; ++i) { add(i, 1); } cout << solve() << '\n'; while (q--) { int pos; char c; cin >> pos >> c; --pos; add(pos, -1); st[pos] = c; add(pos, 1); 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 | #include <bits/stdc++.h> using namespace std; const int N = 1e6 + 50; const int mod = 1e9 + 7; inline int inc(int x, int y) { x += y - mod; return x + (x >> 31 & mod); } inline int dec(int x, int y) { x -= y; return x + (x >> 31 & mod); } inline int mul(int x, int y) { return 1ll * x * y % mod; } auto inc(const auto &first, const auto ...args) { return inc(first, inc(args...)); } auto mul(const auto &first, const auto ...args) { return mul(first, mul(args...)); } inline void upd(int &x, int y) { x = inc(x, y); } inline void dpu(int &x, int y) { x = dec(x, y); } inline void pud(int &x, int y) { x = mul(x, y); } int n, q, su, s[N][3], x, y, cnt; string st; void add(int p, int v) { switch (st[p]) { case 'C': su -= v; (p % 2 ? x : y) += v; break; case 'Z': su += v; (p % 2 ? y : x) += v; break; case 'N': su -= v; cnt += v; x += v; y += v; break; default: throw; } } int64_t solve() { int o = (su % 3 + 3) % 3; int ways = 0; for (int i : {0, 1, 2}) if (i != o) upd(ways, s[cnt][i]); if (n == 1) return ways; if (n % 2 && x == n) --ways; if (n % 2 && y == n) --ways; return (ways % mod + mod) % mod; }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin >> n >> q; s[0][0] = 1; for (int i = 1; i <= n; ++i) { s[i][0] = inc(s[i - 1][0], s[i - 1][2]); s[i][1] = inc(s[i - 1][0], s[i - 1][1]); s[i][2] = inc(s[i - 1][1], s[i - 1][2]); } cin >> st; for (int i = 0; i < n; ++i) { add(i, 1); } cout << solve() << '\n'; while (q--) { int pos; char c; cin >> pos >> c; --pos; add(pos, -1); st[pos] = c; add(pos, 1); cout << solve() << '\n'; } } |