#include <bits/stdc++.h> #define F first #define S second #define ll long long using namespace std; const int MAXN = 2e5 + 99; const ll MOD = 1e9 + 7; int t[MAXN]; int g[MAXN][3]; int cntN; pair<int,int> cntZ,cntC; int n,q; void pre(){ g[0][0] = 0; g[0][1] = 1; g[0][2] = 1; for(int i = 1; i < MAXN; i++){ g[i][0] = (g[i-1][1] + g[i-1][2]) % MOD; g[i][1] = (g[i-1][0] + g[i-1][2]) % MOD; g[i][2] = (g[i-1][0] + g[i-1][1]) % MOD; } } void solve(){ int sumZ = cntZ.F + cntZ.S; int sumC = cntC.F + cntC.S; ll wynik = (g[cntN][((sumZ - sumC) % 3 + 3 ) % 3]); if(cntZ.F == 0 && cntC.S == 0 && n % 2 != 0 && n > 1) wynik--; if(cntZ.S == 0 && cntC.F == 0 && n % 2 != 0 && n > 1) wynik--; cout << max(wynik,(ll)0) << "\n"; } int main(){ cntN = 0; cntZ = cntC = {0,0}; cin >> n >> q; pre(); for(int i = 0; i < n; i++){ char c; cin >> c; if(c == 'C'){ t[i] = 0; cntC.F += (i % 2 == 0); cntC.S += (i % 2 == 1); }else if(c == 'Z'){ t[i] = 1; cntZ.F += (i % 2 == 0); cntZ.S += (i % 2 == 1); }else if(c == 'N'){ t[i] = 2; cntN++; } } solve(); for(int i = 0; i < q; i++){ int j; char c; cin >> j >> c; if(t[j-1] == 0){ cntC.F -= ((j - 1) % 2 == 0); cntC.S -= ((j - 1) % 2 == 1); }else if(t[j-1] == 1){ cntZ.F -= ((j - 1) % 2 == 0); cntZ.S -= ((j - 1) % 2 == 1); }else if(t[j-1] == 2){ cntN--; } if(c == 'C'){ t[j - 1] = 0; cntC.F += ((j - 1) % 2 == 0); cntC.S += ((j - 1) % 2 == 1); }else if(c == 'Z'){ t[(j - 1)] = 1; cntZ.F += ((j - 1) % 2 == 0); cntZ.S += ((j - 1) % 2 == 1); }else if(c == 'N'){ t[(j - 1)] = 2; cntN++; } 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 | #include <bits/stdc++.h> #define F first #define S second #define ll long long using namespace std; const int MAXN = 2e5 + 99; const ll MOD = 1e9 + 7; int t[MAXN]; int g[MAXN][3]; int cntN; pair<int,int> cntZ,cntC; int n,q; void pre(){ g[0][0] = 0; g[0][1] = 1; g[0][2] = 1; for(int i = 1; i < MAXN; i++){ g[i][0] = (g[i-1][1] + g[i-1][2]) % MOD; g[i][1] = (g[i-1][0] + g[i-1][2]) % MOD; g[i][2] = (g[i-1][0] + g[i-1][1]) % MOD; } } void solve(){ int sumZ = cntZ.F + cntZ.S; int sumC = cntC.F + cntC.S; ll wynik = (g[cntN][((sumZ - sumC) % 3 + 3 ) % 3]); if(cntZ.F == 0 && cntC.S == 0 && n % 2 != 0 && n > 1) wynik--; if(cntZ.S == 0 && cntC.F == 0 && n % 2 != 0 && n > 1) wynik--; cout << max(wynik,(ll)0) << "\n"; } int main(){ cntN = 0; cntZ = cntC = {0,0}; cin >> n >> q; pre(); for(int i = 0; i < n; i++){ char c; cin >> c; if(c == 'C'){ t[i] = 0; cntC.F += (i % 2 == 0); cntC.S += (i % 2 == 1); }else if(c == 'Z'){ t[i] = 1; cntZ.F += (i % 2 == 0); cntZ.S += (i % 2 == 1); }else if(c == 'N'){ t[i] = 2; cntN++; } } solve(); for(int i = 0; i < q; i++){ int j; char c; cin >> j >> c; if(t[j-1] == 0){ cntC.F -= ((j - 1) % 2 == 0); cntC.S -= ((j - 1) % 2 == 1); }else if(t[j-1] == 1){ cntZ.F -= ((j - 1) % 2 == 0); cntZ.S -= ((j - 1) % 2 == 1); }else if(t[j-1] == 2){ cntN--; } if(c == 'C'){ t[j - 1] = 0; cntC.F += ((j - 1) % 2 == 0); cntC.S += ((j - 1) % 2 == 1); }else if(c == 'Z'){ t[(j - 1)] = 1; cntZ.F += ((j - 1) % 2 == 0); cntZ.S += ((j - 1) % 2 == 1); }else if(c == 'N'){ t[(j - 1)] = 2; cntN++; } solve(); } } |