#include <bits/stdc++.h> #define ll long long using namespace std; ll mod = 1e9+7; ll dp[200011][3]; int main(){ ios_base::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr); ll n, q; cin>>n>>q; ll ileC=0, ileZ=0, ileN=0; ll parC=0, parZ=0; dp[0][0] = 1; for(ll i = 1; i <= n; i++){ dp[i][0] = (dp[i-1][1] + dp[i-1][2])%mod; dp[i][1] = (dp[i-1][0] + dp[i-1][2])%mod; dp[i][2] = (dp[i-1][1] + dp[i-1][0])%mod; } string s; cin>>s; for(ll i = 0; i < n; i++){ if(s[i]=='C') ileC++; if(s[i]=='Z') ileZ++; if(s[i]=='N') ileN++; if(i%2==0){ if(s[i]=='C') parC++; if(s[i]=='Z') parZ++; } } //cout<<ileC<<" "<<ileZ<<" "<<ileN<<" "<<parC<<" "<<parZ<<'\n'; ll out = dp[ileN][(ileC-ileZ+3*n+1)%3]+dp[ileN][(ileC-ileZ+3*n+2)%3]; if((ileC-ileZ+3*n)%3 >= 0 && n%2 == 1){ if(parC==0&&parZ==ileZ) out--; if(parZ==0&&parC==ileC) out--; } out+=mod; cout<<out%mod<<'\n'; for(ll i = 0; i < q; i++){ ll x; char c; cin>>x>>c; x--; if(s[x]=='C') ileC--; if(s[x]=='Z') ileZ--; if(s[x]=='N') ileN--; if(x%2==0){ if(s[x]=='C') parC--; if(s[x]=='Z') parZ--; } s[x] = c; if(s[x]=='C') ileC++; if(s[x]=='Z') ileZ++; if(s[x]=='N') ileN++; if(x%2==0){ if(s[x]=='C') parC++; if(s[x]=='Z') parZ++; } out = dp[ileN][(ileC-ileZ+3*n+1)%3]+dp[ileN][(ileC-ileZ+3*n+2)%3]; if((ileC-ileZ+3*n)%3 >=0 && n%2 == 1){ if(parC==0&&parZ==ileZ) out--; if(parZ==0&&parC==ileC) out--; } out+=mod; cout<<out%mod<<'\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 | #include <bits/stdc++.h> #define ll long long using namespace std; ll mod = 1e9+7; ll dp[200011][3]; int main(){ ios_base::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr); ll n, q; cin>>n>>q; ll ileC=0, ileZ=0, ileN=0; ll parC=0, parZ=0; dp[0][0] = 1; for(ll i = 1; i <= n; i++){ dp[i][0] = (dp[i-1][1] + dp[i-1][2])%mod; dp[i][1] = (dp[i-1][0] + dp[i-1][2])%mod; dp[i][2] = (dp[i-1][1] + dp[i-1][0])%mod; } string s; cin>>s; for(ll i = 0; i < n; i++){ if(s[i]=='C') ileC++; if(s[i]=='Z') ileZ++; if(s[i]=='N') ileN++; if(i%2==0){ if(s[i]=='C') parC++; if(s[i]=='Z') parZ++; } } //cout<<ileC<<" "<<ileZ<<" "<<ileN<<" "<<parC<<" "<<parZ<<'\n'; ll out = dp[ileN][(ileC-ileZ+3*n+1)%3]+dp[ileN][(ileC-ileZ+3*n+2)%3]; if((ileC-ileZ+3*n)%3 >= 0 && n%2 == 1){ if(parC==0&&parZ==ileZ) out--; if(parZ==0&&parC==ileC) out--; } out+=mod; cout<<out%mod<<'\n'; for(ll i = 0; i < q; i++){ ll x; char c; cin>>x>>c; x--; if(s[x]=='C') ileC--; if(s[x]=='Z') ileZ--; if(s[x]=='N') ileN--; if(x%2==0){ if(s[x]=='C') parC--; if(s[x]=='Z') parZ--; } s[x] = c; if(s[x]=='C') ileC++; if(s[x]=='Z') ileZ++; if(s[x]=='N') ileN++; if(x%2==0){ if(s[x]=='C') parC++; if(s[x]=='Z') parZ++; } out = dp[ileN][(ileC-ileZ+3*n+1)%3]+dp[ileN][(ileC-ileZ+3*n+2)%3]; if((ileC-ileZ+3*n)%3 >=0 && n%2 == 1){ if(parC==0&&parZ==ileZ) out--; if(parZ==0&&parC==ileC) out--; } out+=mod; cout<<out%mod<<'\n'; } return 0; } |