#include <algorithm> #include <cassert> #include <cstdio> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <ranges> #include <set> #include <string> #include <vector> using namespace std; const int MOD = 1e9 + 7; template <int M> struct modint { int v; modint(int xx = 0) : v(xx % M) { v += (M & (0 - (v < 0))); } friend istream& operator>>(istream& i, modint& n) { return i >> n.v; } friend ostream& operator<<(ostream& o, const modint n) { return o << n.v; } modint& operator+=(const modint n) { v += n.v; v -= (M & (0 - (v >= M))); return *this; } modint& operator-=(const modint n) { v -= n.v; v += (M & (0 - (v < 0))); return *this; } friend modint operator+(const modint n, const modint m) { return modint(n) += m; } friend modint operator-(const modint n, const modint m) { return modint(n) -= m; } }; using modi = modint<MOD>; vector<vector<int>> memo; modi f3(int n, int parity) { parity = ((parity % 3) + 3) % 3; if (n == 0) return parity == 0; if (memo[n][parity] != -1) return memo[n][parity]; modi res = f3(n - 1, parity) + f3(n - 1, parity - 1); return memo[n][parity] = res.v; } int n; modi f2(int x, int y) { int z = n - (x + y); modi res = 0; for (int d = 0; d < 3; d++) { if ((x + d + 2 * (y + z - d) + 2) % 3 != 0) continue; res += f3(z, d); } return res; } int q; string s; int x, y; vector<int> broken(2); bool can_break; modi f() { int d = 0; d += can_break && (broken[0] == n); d += can_break && (broken[1] == n); return f2(x, y) + f2(y, x) - d; } void update(int i, bool added) { char c = s[i]; int d = added ? 1 : -1; if (c == 'C') { x += d; broken[(i + 1) % 2] += d; } else if (c == 'Z') { y += d; broken[i % 2] += d; } else { assert(c == 'N'); for (auto& p : broken) p += d; } } void solve() { cin >> n >> q; cin >> s; assert(s.size() == n); int a = ((n + 1) / 2); int b = n - a; can_break = n > 1 && ((a + 2 * b) % 3 == 1); memo.resize(n + 1); for (auto& u : memo) { u.resize(3); for (auto& v : u) v = -1; } for (int i = 0; i < n; i++) update(i, true); cout << f() << "\n"; while (q--) { int k; string c; cin >> k >> c; k--; assert(c.size() == 1); update(k, false); s[k] = c[0]; update(k, true); cout << f() << "\n"; } } int main() { ios::sync_with_stdio(false); cin.exceptions(cin.failbit); cin.tie(0); solve(); }
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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | #include <algorithm> #include <cassert> #include <cstdio> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <ranges> #include <set> #include <string> #include <vector> using namespace std; const int MOD = 1e9 + 7; template <int M> struct modint { int v; modint(int xx = 0) : v(xx % M) { v += (M & (0 - (v < 0))); } friend istream& operator>>(istream& i, modint& n) { return i >> n.v; } friend ostream& operator<<(ostream& o, const modint n) { return o << n.v; } modint& operator+=(const modint n) { v += n.v; v -= (M & (0 - (v >= M))); return *this; } modint& operator-=(const modint n) { v -= n.v; v += (M & (0 - (v < 0))); return *this; } friend modint operator+(const modint n, const modint m) { return modint(n) += m; } friend modint operator-(const modint n, const modint m) { return modint(n) -= m; } }; using modi = modint<MOD>; vector<vector<int>> memo; modi f3(int n, int parity) { parity = ((parity % 3) + 3) % 3; if (n == 0) return parity == 0; if (memo[n][parity] != -1) return memo[n][parity]; modi res = f3(n - 1, parity) + f3(n - 1, parity - 1); return memo[n][parity] = res.v; } int n; modi f2(int x, int y) { int z = n - (x + y); modi res = 0; for (int d = 0; d < 3; d++) { if ((x + d + 2 * (y + z - d) + 2) % 3 != 0) continue; res += f3(z, d); } return res; } int q; string s; int x, y; vector<int> broken(2); bool can_break; modi f() { int d = 0; d += can_break && (broken[0] == n); d += can_break && (broken[1] == n); return f2(x, y) + f2(y, x) - d; } void update(int i, bool added) { char c = s[i]; int d = added ? 1 : -1; if (c == 'C') { x += d; broken[(i + 1) % 2] += d; } else if (c == 'Z') { y += d; broken[i % 2] += d; } else { assert(c == 'N'); for (auto& p : broken) p += d; } } void solve() { cin >> n >> q; cin >> s; assert(s.size() == n); int a = ((n + 1) / 2); int b = n - a; can_break = n > 1 && ((a + 2 * b) % 3 == 1); memo.resize(n + 1); for (auto& u : memo) { u.resize(3); for (auto& v : u) v = -1; } for (int i = 0; i < n; i++) update(i, true); cout << f() << "\n"; while (q--) { int k; string c; cin >> k >> c; k--; assert(c.size() == 1); update(k, false); s[k] = c[0]; update(k, true); cout << f() << "\n"; } } int main() { ios::sync_with_stdio(false); cin.exceptions(cin.failbit); cin.tie(0); solve(); } |