#include <bits/stdc++.h> using namespace std; using ll = long long; #ifdef DEBUG #include "debug.h" #else #define debug(...) ; #endif #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) #define rep(i, n) for (int i = 0; i < (n); ++i) #define repp(i, n, m) for (int i = (n); i < (m); ++i) #define repr(i, n) for (int i = (n) - 1; i >= 0; --i) #define reppr(i, n, m) for (int i = (m) - 1; i >= (n); --i) using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using pi = pair<int, int>; using pll = pair<ll, ll>; template <typename T, typename V> void mini(T& a, V b) {if (b < a) a = b; } template <typename T, typename V> void maxi(T& a, V b) {if (b > a) a = b; } const int MOD = 1e9 + 7; struct modint { int x; modint operator+(modint o) { return {(x + o.x) % MOD}; } modint operator*(modint o) { return {ll(x) * o.x % MOD}; } }; void __dbg(modint m) { cout << m.x; } using tab = array<modint, 3>; tab comb(tab a, tab b) { return { a[0]*b[0] + a[1]*b[2] + a[2]*b[1], a[0]*b[1] + a[1]*b[0] + a[2]*b[2], a[0]*b[2] + a[1]*b[1] + a[2]*b[0] }; } int mp(char ch) { if (ch == 'C') return 0; if (ch == 'Z') return 1; if (ch == 'N') return 2; assert(false); } void solve() { int n, q; cin >> n >> q; string s; cin >> s; int cnt[3] = {0, 0, 0}; int badAlternating[2] = {0, 0}; for (int i = 0; i < n; ++i) { int ch = mp(s[i]); ++cnt[ch]; if (ch != 2) ++badAlternating[1&(ch^i)]; } vector<vector<tab>> V; V.emplace_back(vector<tab>{tab{{modint{1}, modint{0}, modint{0}}}, tab{{modint{0}, modint{1}, modint{0}}}}); V.emplace_back(vector<tab>{tab{{modint{1}, modint{0}, modint{0}}}, tab{{modint{0}, modint{0}, modint{1}}}}); V.emplace_back(vector<tab>{tab{{modint{1}, modint{0}, modint{0}}}, tab{{modint{0}, modint{1}, modint{1}}}}); for (auto& vec : V) { while (vec.size() <= n) vec.push_back(comb(vec[1], vec.back())); } auto calc = [&](){ debug(cnt[0],cnt[1],cnt[2],badAlternating[0],badAlternating[1]); tab tot = comb(comb(V[0][cnt[0]], V[1][cnt[1]]), V[2][cnt[2]]); modint nonzero = tot[1] + tot[2]; nonzero.x -= bool((n&1) && !badAlternating[0] && n>1); nonzero.x -= bool((n&1) && !badAlternating[1] && n>1); nonzero = nonzero + modint{MOD}; return nonzero.x; }; cout << calc() << '\n'; while (q--) { int i; char ch; cin >> i >> ch; --i; if (mp(s[i]) != 2) --badAlternating[1&(mp(s[i])^i)]; --cnt[mp(s[i])]; s[i] = ch; if (mp(s[i]) != 2) ++badAlternating[1&(mp(s[i])^i)]; ++cnt[mp(s[i])]; cout << calc() << '\n'; } } int main() { #ifndef DEBUG ios_base::sync_with_stdio(false); cin.tie(nullptr); #endif int z = 1; // scanf("%d", &z); while (z--) solve(); 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 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 137 138 139 140 | #include <bits/stdc++.h> using namespace std; using ll = long long; #ifdef DEBUG #include "debug.h" #else #define debug(...) ; #endif #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) #define rep(i, n) for (int i = 0; i < (n); ++i) #define repp(i, n, m) for (int i = (n); i < (m); ++i) #define repr(i, n) for (int i = (n) - 1; i >= 0; --i) #define reppr(i, n, m) for (int i = (m) - 1; i >= (n); --i) using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using pi = pair<int, int>; using pll = pair<ll, ll>; template <typename T, typename V> void mini(T& a, V b) {if (b < a) a = b; } template <typename T, typename V> void maxi(T& a, V b) {if (b > a) a = b; } const int MOD = 1e9 + 7; struct modint { int x; modint operator+(modint o) { return {(x + o.x) % MOD}; } modint operator*(modint o) { return {ll(x) * o.x % MOD}; } }; void __dbg(modint m) { cout << m.x; } using tab = array<modint, 3>; tab comb(tab a, tab b) { return { a[0]*b[0] + a[1]*b[2] + a[2]*b[1], a[0]*b[1] + a[1]*b[0] + a[2]*b[2], a[0]*b[2] + a[1]*b[1] + a[2]*b[0] }; } int mp(char ch) { if (ch == 'C') return 0; if (ch == 'Z') return 1; if (ch == 'N') return 2; assert(false); } void solve() { int n, q; cin >> n >> q; string s; cin >> s; int cnt[3] = {0, 0, 0}; int badAlternating[2] = {0, 0}; for (int i = 0; i < n; ++i) { int ch = mp(s[i]); ++cnt[ch]; if (ch != 2) ++badAlternating[1&(ch^i)]; } vector<vector<tab>> V; V.emplace_back(vector<tab>{tab{{modint{1}, modint{0}, modint{0}}}, tab{{modint{0}, modint{1}, modint{0}}}}); V.emplace_back(vector<tab>{tab{{modint{1}, modint{0}, modint{0}}}, tab{{modint{0}, modint{0}, modint{1}}}}); V.emplace_back(vector<tab>{tab{{modint{1}, modint{0}, modint{0}}}, tab{{modint{0}, modint{1}, modint{1}}}}); for (auto& vec : V) { while (vec.size() <= n) vec.push_back(comb(vec[1], vec.back())); } auto calc = [&](){ debug(cnt[0],cnt[1],cnt[2],badAlternating[0],badAlternating[1]); tab tot = comb(comb(V[0][cnt[0]], V[1][cnt[1]]), V[2][cnt[2]]); modint nonzero = tot[1] + tot[2]; nonzero.x -= bool((n&1) && !badAlternating[0] && n>1); nonzero.x -= bool((n&1) && !badAlternating[1] && n>1); nonzero = nonzero + modint{MOD}; return nonzero.x; }; cout << calc() << '\n'; while (q--) { int i; char ch; cin >> i >> ch; --i; if (mp(s[i]) != 2) --badAlternating[1&(mp(s[i])^i)]; --cnt[mp(s[i])]; s[i] = ch; if (mp(s[i]) != 2) ++badAlternating[1&(mp(s[i])^i)]; ++cnt[mp(s[i])]; cout << calc() << '\n'; } } int main() { #ifndef DEBUG ios_base::sync_with_stdio(false); cin.tie(nullptr); #endif int z = 1; // scanf("%d", &z); while (z--) solve(); return 0; } |