#include <bits/stdc++.h> using namespace std; typedef long long LL; typedef long double LD; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; #define all(x) (x).begin(), (x).end() #define sz(x) (int)(x).size() template<typename _T> inline void _DBG(const char *s, _T x) { cerr << s << " = " << x << "\n"; } template<typename _T, typename... args> void _DBG(const char *s, _T x, args... a) { while(*s != ',') cerr << *s++; cerr << " = " << x << ','; _DBG(s + 1, a...); } #ifdef LOCAL #define _upgrade ios_base::sync_with_stdio(0); #define DBG(...) _DBG(#__VA_ARGS__, __VA_ARGS__) #else #define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define DBG(...) (__VA_ARGS__) #define cerr if(0) cout #endif // ********************** CODE ********************** // const int N = 2e5 + 7; const int M = 1e9 + 7; int n, q, dp[N][3]; string s; int cnt_c[3], cnt_z[3], cnt_n[3]; void update_cnt(char c, int val, int i) { if (c == 'C') { cnt_c[i % 2] += val; cnt_c[2] += val; } if (c == 'Z') { cnt_z[i % 2] += val; cnt_z[2] += val; } if (c == 'N') { cnt_n[i % 2] += val; cnt_n[2] += val; } } int calc_ans() { int r = ((cnt_c[2] - cnt_z[2]) % 3 + 3) % 3; int t = (dp[cnt_n[2]][0] + dp[cnt_n[2]][1] + dp[cnt_n[2]][2] - dp[cnt_n[2]][r]) % M; if (n % 2 == 1 && n > 1 && cnt_c[0] == 0 && cnt_z[1] == 0) t = (t + M - 1) % M; if (n % 2 == 1 && n > 1 && cnt_c[1] == 0 && cnt_z[0] == 0) t = (t + M - 1) % M; return t; } int main() { _upgrade cin >> n >> q >> s; dp[0][0] = 1; for (int i = 1; i <= n; i++) { dp[i][0] = (dp[i - 1][1] + dp[i - 1][2]) % M; dp[i][1] = (dp[i - 1][0] + dp[i - 1][2]) % M; dp[i][2] = (dp[i - 1][0] + dp[i - 1][1]) % M; } for (int i = 0; i < n; i++) update_cnt(s[i], +1, i); cout << calc_ans() << "\n"; while (q--) { int id; char c; cin >> id >> c; id--; update_cnt(s[id], -1, id); s[id] = c; update_cnt(s[id], +1, id); cout << calc_ans() << "\n"; } 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 | #include <bits/stdc++.h> using namespace std; typedef long long LL; typedef long double LD; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; #define all(x) (x).begin(), (x).end() #define sz(x) (int)(x).size() template<typename _T> inline void _DBG(const char *s, _T x) { cerr << s << " = " << x << "\n"; } template<typename _T, typename... args> void _DBG(const char *s, _T x, args... a) { while(*s != ',') cerr << *s++; cerr << " = " << x << ','; _DBG(s + 1, a...); } #ifdef LOCAL #define _upgrade ios_base::sync_with_stdio(0); #define DBG(...) _DBG(#__VA_ARGS__, __VA_ARGS__) #else #define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define DBG(...) (__VA_ARGS__) #define cerr if(0) cout #endif // ********************** CODE ********************** // const int N = 2e5 + 7; const int M = 1e9 + 7; int n, q, dp[N][3]; string s; int cnt_c[3], cnt_z[3], cnt_n[3]; void update_cnt(char c, int val, int i) { if (c == 'C') { cnt_c[i % 2] += val; cnt_c[2] += val; } if (c == 'Z') { cnt_z[i % 2] += val; cnt_z[2] += val; } if (c == 'N') { cnt_n[i % 2] += val; cnt_n[2] += val; } } int calc_ans() { int r = ((cnt_c[2] - cnt_z[2]) % 3 + 3) % 3; int t = (dp[cnt_n[2]][0] + dp[cnt_n[2]][1] + dp[cnt_n[2]][2] - dp[cnt_n[2]][r]) % M; if (n % 2 == 1 && n > 1 && cnt_c[0] == 0 && cnt_z[1] == 0) t = (t + M - 1) % M; if (n % 2 == 1 && n > 1 && cnt_c[1] == 0 && cnt_z[0] == 0) t = (t + M - 1) % M; return t; } int main() { _upgrade cin >> n >> q >> s; dp[0][0] = 1; for (int i = 1; i <= n; i++) { dp[i][0] = (dp[i - 1][1] + dp[i - 1][2]) % M; dp[i][1] = (dp[i - 1][0] + dp[i - 1][2]) % M; dp[i][2] = (dp[i - 1][0] + dp[i - 1][1]) % M; } for (int i = 0; i < n; i++) update_cnt(s[i], +1, i); cout << calc_ans() << "\n"; while (q--) { int id; char c; cin >> id >> c; id--; update_cnt(s[id], -1, id); s[id] = c; update_cnt(s[id], +1, id); cout << calc_ans() << "\n"; } return 0; } |