#include <bits/stdc++.h> #define mod 1000000007 #define inv3 333333336 using namespace std; typedef long long ll; char seq[200010]; int pow2[200010]; int a[6]={2, 1, -1, -2, -1, 1}; int colours[3]; int sth[2][2]; int f(int n, int k) { return (((ll)pow2[n]+a[(6+(n-2*k)%6)%6])*inv3)%mod; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n,q; cin>>n>>q; pow2[0]=1; for(int i=1; i<=n; i++) pow2[i]=(2*pow2[i-1])%mod; for(int i=0; i<n; i++) { cin>>seq[i]; if(seq[i]=='C') { sth[i%2][0]++; colours[0]++; } else if(seq[i]=='Z') { sth[i%2][1]++; colours[1]++; } else { colours[2]++; } } int res=(mod+(pow2[colours[2]]-f(colours[2],((colours[2]-(colours[1]-colours[0]))*2)%3))%mod)%mod; // cout<<res<<" "<<pow2[colours[2]]<<" "<<((colours[2]-(colours[1]-colours[0]))*2)%3<<"\n"; if(n==1) cout<<res; else if(colours[2]==n) { if(n%2==0) cout<<res; else cout<<res-2; } else if((sth[0][0]==0&&sth[1][1]==0)||(sth[1][0]==0&&sth[0][1]==0)) { if(n%2==0) cout<<res; else cout<<res-1; } else cout<<res; cout<<"\n"; for(int i=0; i<q; i++) { int p; char c; cin>>p>>c; p-=1; if(seq[p]=='C') { sth[p%2][0]--; colours[0]--; } else if(seq[p]=='Z') { sth[p%2][1]--; colours[1]--; } else { colours[2]--; } if(c=='C') { sth[p%2][0]++; colours[0]++; } else if(c=='Z') { sth[p%2][1]++; colours[1]++; } else { colours[2]++; } seq[p]=c; int res=(mod+(pow2[colours[2]]-f(colours[2],((colours[2]-(colours[1]-colours[0]))*2)%3))%mod)%mod; if(n==1) cout<<res; else if(colours[2]==n) { if(n%2==0) cout<<res; else cout<<res-2; } else if((sth[0][0]==0&&sth[1][1]==0)||(sth[1][0]==0&&sth[0][1]==0)) { if(n%2==0) cout<<res; else cout<<res-1; } else cout<<res; cout<<"\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 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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | #include <bits/stdc++.h> #define mod 1000000007 #define inv3 333333336 using namespace std; typedef long long ll; char seq[200010]; int pow2[200010]; int a[6]={2, 1, -1, -2, -1, 1}; int colours[3]; int sth[2][2]; int f(int n, int k) { return (((ll)pow2[n]+a[(6+(n-2*k)%6)%6])*inv3)%mod; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n,q; cin>>n>>q; pow2[0]=1; for(int i=1; i<=n; i++) pow2[i]=(2*pow2[i-1])%mod; for(int i=0; i<n; i++) { cin>>seq[i]; if(seq[i]=='C') { sth[i%2][0]++; colours[0]++; } else if(seq[i]=='Z') { sth[i%2][1]++; colours[1]++; } else { colours[2]++; } } int res=(mod+(pow2[colours[2]]-f(colours[2],((colours[2]-(colours[1]-colours[0]))*2)%3))%mod)%mod; // cout<<res<<" "<<pow2[colours[2]]<<" "<<((colours[2]-(colours[1]-colours[0]))*2)%3<<"\n"; if(n==1) cout<<res; else if(colours[2]==n) { if(n%2==0) cout<<res; else cout<<res-2; } else if((sth[0][0]==0&&sth[1][1]==0)||(sth[1][0]==0&&sth[0][1]==0)) { if(n%2==0) cout<<res; else cout<<res-1; } else cout<<res; cout<<"\n"; for(int i=0; i<q; i++) { int p; char c; cin>>p>>c; p-=1; if(seq[p]=='C') { sth[p%2][0]--; colours[0]--; } else if(seq[p]=='Z') { sth[p%2][1]--; colours[1]--; } else { colours[2]--; } if(c=='C') { sth[p%2][0]++; colours[0]++; } else if(c=='Z') { sth[p%2][1]++; colours[1]++; } else { colours[2]++; } seq[p]=c; int res=(mod+(pow2[colours[2]]-f(colours[2],((colours[2]-(colours[1]-colours[0]))*2)%3))%mod)%mod; if(n==1) cout<<res; else if(colours[2]==n) { if(n%2==0) cout<<res; else cout<<res-2; } else if((sth[0][0]==0&&sth[1][1]==0)||(sth[1][0]==0&&sth[0][1]==0)) { if(n%2==0) cout<<res; else cout<<res-1; } else cout<<res; cout<<"\n"; } } |