#include <bits/stdc++.h> #ifdef LOC #include "debuglib.h" #else #define deb(...) #define DBP(...) #endif using namespace std; using ll = long long; using vi = vector<int>; using pii = pair<int, int>; #define pb push_back #define mp make_pair #define x first #define y second #define rep(i, b, e) for (int i = (b); i < (e); i++) #define each(a, x) for (auto& a : (x)) #define all(x) (x).begin(), (x).end() #define sz(x) int((x).size()) constexpr int MOD = 1e9+7; void run() { int n, q; string s; cin >> n >> q >> s; int cnt[3] = {}; int aba = 0, bab = 0; vector<array<ll, 3>> changes(n+1); changes[0] = {1, 0, 0}; rep(i, 1, sz(changes)) { rep(v, 0, 3) { changes[i][v] = (changes[i-1][(v+1)%3] + changes[i-1][(v+2)%3]) % MOD; } } auto get = [&](int i) { return s[i] == 'C' ? 0 : s[i] == 'Z' ? 1 : 2; }; auto update = [&](int i, int sign) { int v = get(i); cnt[v] += sign; aba += (v == 2 || v == i%2) * sign; bab += (v == 2 || v != i%2) * sign; }; auto answer = [&]() { int fixed = (cnt[0]-cnt[1]+n*3) % 3; ll ways = changes[cnt[2]][(4-fixed)%3]; ways += changes[cnt[2]][(5-fixed)%3]; if (n > 1 && n%2 == 1) { ways -= (aba == n); ways -= (bab == n); } ways %= MOD; if (ways < 0) ways += MOD; return ways; }; rep(i, 0, n) { update(i, 1); } cout << answer() << '\n'; while (q--) { int i; char c; cin >> i >> c; i--; update(i, -1); s[i] = c; update(i, 1); cout << answer() << '\n'; } } int main() { cin.sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(10); run(); cout << flush; _Exit(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 | #include <bits/stdc++.h> #ifdef LOC #include "debuglib.h" #else #define deb(...) #define DBP(...) #endif using namespace std; using ll = long long; using vi = vector<int>; using pii = pair<int, int>; #define pb push_back #define mp make_pair #define x first #define y second #define rep(i, b, e) for (int i = (b); i < (e); i++) #define each(a, x) for (auto& a : (x)) #define all(x) (x).begin(), (x).end() #define sz(x) int((x).size()) constexpr int MOD = 1e9+7; void run() { int n, q; string s; cin >> n >> q >> s; int cnt[3] = {}; int aba = 0, bab = 0; vector<array<ll, 3>> changes(n+1); changes[0] = {1, 0, 0}; rep(i, 1, sz(changes)) { rep(v, 0, 3) { changes[i][v] = (changes[i-1][(v+1)%3] + changes[i-1][(v+2)%3]) % MOD; } } auto get = [&](int i) { return s[i] == 'C' ? 0 : s[i] == 'Z' ? 1 : 2; }; auto update = [&](int i, int sign) { int v = get(i); cnt[v] += sign; aba += (v == 2 || v == i%2) * sign; bab += (v == 2 || v != i%2) * sign; }; auto answer = [&]() { int fixed = (cnt[0]-cnt[1]+n*3) % 3; ll ways = changes[cnt[2]][(4-fixed)%3]; ways += changes[cnt[2]][(5-fixed)%3]; if (n > 1 && n%2 == 1) { ways -= (aba == n); ways -= (bab == n); } ways %= MOD; if (ways < 0) ways += MOD; return ways; }; rep(i, 0, n) { update(i, 1); } cout << answer() << '\n'; while (q--) { int i; char c; cin >> i >> c; i--; update(i, -1); s[i] = c; update(i, 1); cout << answer() << '\n'; } } int main() { cin.sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(10); run(); cout << flush; _Exit(0); } |