#include <iostream> #include <stdio.h> #define N 200000 using namespace std; int f(char c) { if(c=='C') return 0; if(c=='Z') return 1; return 2; } int n,q; int a[N+1],p[3][2]; long long M=1000000007,inv2=500000004,pw=1; long long dp[N+1][3]; int idx; char ch; long long res() { /*cout<<"res()"<<endl; for(int i=0;i<3;i++) { cout<<p[i][0]<<" "<<p[i][1]<<endl; }*/ int d=abs(p[0][0]+p[0][1]-p[1][0]-p[1][1])%3; //cout<<d<<endl; long long r=(pw-dp[p[2][0]+p[2][1]][d]+M)%M; if((n&1)&&(n>1)) { if(p[0][0]==0&&p[1][1]==0) r--; if(p[0][1]==0&&p[1][0]==0) r--; r=(r+M)%M; } return r; } int main() { dp[0][0]=1; for(int i=1;i<=N;i++) { dp[i][0]=(dp[i-1][1]+dp[i-1][2])%M; dp[i][1]=(dp[i-1][2]+dp[i-1][0])%M; dp[i][2]=(dp[i-1][0]+dp[i-1][1])%M; //if(i<10) cout<<dp[i][0]<<" "<<dp[i][1]<<" "<<dp[i][2]<<endl; } scanf("%d%d\n",&n,&q); for(int i=0;i<n;i++) { a[i]=f(getchar()); p[a[i]][i&1]++; if(a[i]==2) pw=(pw*2)%M; } printf("%lld\n",res()); for(int i=0;i<q;i++) { scanf("%d %c\n",&idx,&ch); idx--; p[a[idx]][idx&1]--; if(a[idx]==2) pw=(pw*inv2)%M; a[idx]=f(ch); p[a[idx]][idx&1]++; if(a[idx]==2) pw=(pw*2)%M; printf("%lld\n",res()); } 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 | #include <iostream> #include <stdio.h> #define N 200000 using namespace std; int f(char c) { if(c=='C') return 0; if(c=='Z') return 1; return 2; } int n,q; int a[N+1],p[3][2]; long long M=1000000007,inv2=500000004,pw=1; long long dp[N+1][3]; int idx; char ch; long long res() { /*cout<<"res()"<<endl; for(int i=0;i<3;i++) { cout<<p[i][0]<<" "<<p[i][1]<<endl; }*/ int d=abs(p[0][0]+p[0][1]-p[1][0]-p[1][1])%3; //cout<<d<<endl; long long r=(pw-dp[p[2][0]+p[2][1]][d]+M)%M; if((n&1)&&(n>1)) { if(p[0][0]==0&&p[1][1]==0) r--; if(p[0][1]==0&&p[1][0]==0) r--; r=(r+M)%M; } return r; } int main() { dp[0][0]=1; for(int i=1;i<=N;i++) { dp[i][0]=(dp[i-1][1]+dp[i-1][2])%M; dp[i][1]=(dp[i-1][2]+dp[i-1][0])%M; dp[i][2]=(dp[i-1][0]+dp[i-1][1])%M; //if(i<10) cout<<dp[i][0]<<" "<<dp[i][1]<<" "<<dp[i][2]<<endl; } scanf("%d%d\n",&n,&q); for(int i=0;i<n;i++) { a[i]=f(getchar()); p[a[i]][i&1]++; if(a[i]==2) pw=(pw*2)%M; } printf("%lld\n",res()); for(int i=0;i<q;i++) { scanf("%d %c\n",&idx,&ch); idx--; p[a[idx]][idx&1]--; if(a[idx]==2) pw=(pw*inv2)%M; a[idx]=f(ch); p[a[idx]][idx&1]++; if(a[idx]==2) pw=(pw*2)%M; printf("%lld\n",res()); } return 0; } |