#include<bits/stdc++.h> using namespace std; typedef long long ll; const ll mod=1000000007; const int N=200010; ll pw2[N]; int a[N]; int tot=0; int badA=0,badB=0; int sum=0; int n,q; ll dp[N][3]; ll solve(){ if (n==1){ return pw2[tot]; } ll res=0ll; if (tot==0){ res=(sum!=0); } else { int cur=(sum+tot)%3; res=pw2[tot]; cur=(3-cur)%3; res+=mod-dp[tot][cur]; res%=mod; } if (badA==0 && n%2) res+=mod-1; if (badB==0 && n%2) res+=mod-1; return res%mod; } void upd(int pos,int x){ sum+=3-a[pos]; if (pos%2+1==a[pos]) badA--; if ((pos+1)%2+1==a[pos]) badB--; if (a[pos]==0) tot--; a[pos]=x; sum+=a[pos]; if (pos%2+1==a[pos]) badA++; if ((pos+1)%2+1==a[pos]) badB++; if (a[pos]==0) tot++; sum%=3; } int main(){ ios_base::sync_with_stdio(false); cin.tie(0); cin>>n>>q; dp[0][0]=1; for (int i=0;i<n;i++){ for (int j=0;j<3;j++){ for (int t=0;t<2;t++){ (dp[i+1][(j+t)%3]+=dp[i][j])%=mod; } } } pw2[0]=1; for (int i=1;i<=n;i++) pw2[i]=(pw2[i-1]*2)%mod; tot=n; string s;cin>>s; for (int i=0;i<s.size();i++){ if (s[i]=='C') upd(i,1); else if (s[i]=='Z') upd(i,2); } cout<<solve()<<'\n'; for (int i=1;i<=q;i++){ int pos;cin>>pos; char c;cin>>c; pos--; if (c=='C') upd(pos,1); else if (c=='Z') upd(pos,2); else upd(pos,0); ll ans=solve(); cout<<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 | #include<bits/stdc++.h> using namespace std; typedef long long ll; const ll mod=1000000007; const int N=200010; ll pw2[N]; int a[N]; int tot=0; int badA=0,badB=0; int sum=0; int n,q; ll dp[N][3]; ll solve(){ if (n==1){ return pw2[tot]; } ll res=0ll; if (tot==0){ res=(sum!=0); } else { int cur=(sum+tot)%3; res=pw2[tot]; cur=(3-cur)%3; res+=mod-dp[tot][cur]; res%=mod; } if (badA==0 && n%2) res+=mod-1; if (badB==0 && n%2) res+=mod-1; return res%mod; } void upd(int pos,int x){ sum+=3-a[pos]; if (pos%2+1==a[pos]) badA--; if ((pos+1)%2+1==a[pos]) badB--; if (a[pos]==0) tot--; a[pos]=x; sum+=a[pos]; if (pos%2+1==a[pos]) badA++; if ((pos+1)%2+1==a[pos]) badB++; if (a[pos]==0) tot++; sum%=3; } int main(){ ios_base::sync_with_stdio(false); cin.tie(0); cin>>n>>q; dp[0][0]=1; for (int i=0;i<n;i++){ for (int j=0;j<3;j++){ for (int t=0;t<2;t++){ (dp[i+1][(j+t)%3]+=dp[i][j])%=mod; } } } pw2[0]=1; for (int i=1;i<=n;i++) pw2[i]=(pw2[i-1]*2)%mod; tot=n; string s;cin>>s; for (int i=0;i<s.size();i++){ if (s[i]=='C') upd(i,1); else if (s[i]=='Z') upd(i,2); } cout<<solve()<<'\n'; for (int i=1;i<=q;i++){ int pos;cin>>pos; char c;cin>>c; pos--; if (c=='C') upd(pos,1); else if (c=='Z') upd(pos,2); else upd(pos,0); ll ans=solve(); cout<<ans<<'\n'; } return 0; } |