#include<bits/stdc++.h> using namespace std; typedef long long ll; #define rep(a, b) for(int a = 0; a < (b); ++a) #define st first #define nd second #define pb push_back #define all(a) a.begin(), a.end() const ll MOD=1e9+7; const int LIM=2e5+7; ll dp[LIM][3], ile[2][2], cz, zi, ni, n, q; string s; void upd(int a, int b) { if(s[a]=='C') { cz+=b; ile[0][a%2]+=b; } if(s[a]=='Z') { zi+=b; ile[1][a%2]+=b; } if(s[a]=='N') ni+=b; } ll cnt() { ll ans=0; rep(i, 3) if((i+2*cz+zi)%3!=0) ans=(ans+dp[ni][i])%MOD; bool ok=true; rep(i, 2) if((ile[i][0] && ile[i][1]) || (ile[0][i] && ile[1][i])) ok=false; if(n%2==1 && ok) ans=(ans-1+MOD)%MOD; if(n%2==1 && ni==n) ans=(ans-1+MOD)%MOD; return ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> q >> s; dp[0][0]=1; rep(i, n) rep(j, 3) dp[i+1][j]=(dp[i][(j+1)%3]+dp[i][(j+2)%3])%MOD; rep(i, n) upd(i, 1); cout << cnt() << '\n'; while(q--) { int a; char b; cin >> a >> b; --a; upd(a, -1); s[a]=b; upd(a, 1); cout << cnt() << '\n'; } }
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 | #include<bits/stdc++.h> using namespace std; typedef long long ll; #define rep(a, b) for(int a = 0; a < (b); ++a) #define st first #define nd second #define pb push_back #define all(a) a.begin(), a.end() const ll MOD=1e9+7; const int LIM=2e5+7; ll dp[LIM][3], ile[2][2], cz, zi, ni, n, q; string s; void upd(int a, int b) { if(s[a]=='C') { cz+=b; ile[0][a%2]+=b; } if(s[a]=='Z') { zi+=b; ile[1][a%2]+=b; } if(s[a]=='N') ni+=b; } ll cnt() { ll ans=0; rep(i, 3) if((i+2*cz+zi)%3!=0) ans=(ans+dp[ni][i])%MOD; bool ok=true; rep(i, 2) if((ile[i][0] && ile[i][1]) || (ile[0][i] && ile[1][i])) ok=false; if(n%2==1 && ok) ans=(ans-1+MOD)%MOD; if(n%2==1 && ni==n) ans=(ans-1+MOD)%MOD; return ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> q >> s; dp[0][0]=1; rep(i, n) rep(j, 3) dp[i+1][j]=(dp[i][(j+1)%3]+dp[i][(j+2)%3])%MOD; rep(i, n) upd(i, 1); cout << cnt() << '\n'; while(q--) { int a; char b; cin >> a >> b; --a; upd(a, -1); s[a]=b; upd(a, 1); cout << cnt() << '\n'; } } |