#include <bits/stdc++.h> #define maxn 200005 #define ll long long #define mod 1000000007ll using namespace std; ll dp[maxn][3]; int n, q; char wej[maxn]; int ileC, ileN; int startC, startZ; inline void update(int i, int mnoz) { if(wej[i] == 'C') ileC += mnoz; if(wej[i] == 'N') ileN += mnoz; if(wej[i]=='N' || ((i&1) && wej[i]=='C') || ((!(i&1)) && wej[i]=='Z')) startC += mnoz; if(wej[i]=='N' || ((i&1) && wej[i]=='Z') || ((!(i&1)) && wej[i]=='C')) startZ += mnoz; } inline ll solve() { int t = (2*n-4*ileC)%3; if(t < 0) t += 3; ll ret = dp[ileN][(t+1)%3] + dp[ileN][(t+2)%3]; if(ret >= mod) ret -= mod; if((n>1) && (n&1)) { if(startC == n) --ret; if(startZ == n) --ret; if(ret < 0ll) ret += mod; } return ret; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> q; dp[0][0] = 1ll; for(int i = 0; i < n; ++i) for(int j = 0; j < 3; ++j) { dp[i+1][j] += dp[i][j]; if(dp[i+1][j] >= mod) dp[i+1][j] -= mod; dp[i+1][(j+1)%3] += dp[i][j]; if(dp[i+1][(j+1)%3] >= mod) dp[i+1][(j+1)%3] -= mod; } for(int i = 1; i <= n; ++i) { cin >> wej[i]; update(i, 1); } for(++q; q; --q) { ll wyn = solve(); cout << wyn << "\n"; if(q == 1) break; int i; char c; cin >> i >> c; update(i, -1); wej[i] = c; update(i, 1); } 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 | #include <bits/stdc++.h> #define maxn 200005 #define ll long long #define mod 1000000007ll using namespace std; ll dp[maxn][3]; int n, q; char wej[maxn]; int ileC, ileN; int startC, startZ; inline void update(int i, int mnoz) { if(wej[i] == 'C') ileC += mnoz; if(wej[i] == 'N') ileN += mnoz; if(wej[i]=='N' || ((i&1) && wej[i]=='C') || ((!(i&1)) && wej[i]=='Z')) startC += mnoz; if(wej[i]=='N' || ((i&1) && wej[i]=='Z') || ((!(i&1)) && wej[i]=='C')) startZ += mnoz; } inline ll solve() { int t = (2*n-4*ileC)%3; if(t < 0) t += 3; ll ret = dp[ileN][(t+1)%3] + dp[ileN][(t+2)%3]; if(ret >= mod) ret -= mod; if((n>1) && (n&1)) { if(startC == n) --ret; if(startZ == n) --ret; if(ret < 0ll) ret += mod; } return ret; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> q; dp[0][0] = 1ll; for(int i = 0; i < n; ++i) for(int j = 0; j < 3; ++j) { dp[i+1][j] += dp[i][j]; if(dp[i+1][j] >= mod) dp[i+1][j] -= mod; dp[i+1][(j+1)%3] += dp[i][j]; if(dp[i+1][(j+1)%3] >= mod) dp[i+1][(j+1)%3] -= mod; } for(int i = 1; i <= n; ++i) { cin >> wej[i]; update(i, 1); } for(++q; q; --q) { ll wyn = solve(); cout << wyn << "\n"; if(q == 1) break; int i; char c; cin >> i >> c; update(i, -1); wej[i] = c; update(i, 1); } return 0; } |