#include <bits/stdc++.h> using namespace std; const int MOD=1e9+7, MXN=2e5+1; const long long INVT=333333336; long long pd[MXN]; int cnt[300][4], rep[]={1, 2, 1, -1, -2, -1}; long long bs(int n, int x) { if (x==1) { long long r=(pd[n]-rep[(n+2)%6])*INVT%MOD; if (r<0) r+=MOD; return r; } else if (x==2) { long long r=(pd[n]-rep[n%6])*INVT%MOD; if (r<0) r+=MOD; return r; } else //if(x==0) { long long r=((pd[n]+rep[(n+1)%6])*INVT%MOD); if (r<0) r+=MOD; return r; } } vector<char>a; void solve() { int cw=cnt['C'][0]+cnt['C'][1]+cnt['C'][2]+cnt['C'][3]; int zl=cnt['Z'][0]+cnt['Z'][1]+cnt['Z'][2]+cnt['Z'][3]; int nb=cnt['N'][0]+cnt['N'][1]+cnt['N'][2]+cnt['N'][3]; int x=(cw+2*zl+2*nb)%3, n=cw+zl+nb; //~ cout<<cw<<" "<<zl<<" "<<nb<<" "<<x<<"\n"; long long ans=bs(nb, (x+1)%3)+bs(nb, (x+2)%3); //~ cout<<bs(nb, (x+1)%3)<<" "<<bs(nb, (x+2)%3)<<"\n"; int cw1=cnt['C'][0]+cnt['C'][1], cw2=cnt['C'][0]+cnt['C'][2]; int zl1=cnt['Z'][0]+cnt['Z'][1], zl2=cnt['Z'][0]+cnt['Z'][2]; if (((cw1==cw && zl1==0) || (cw1==0 && zl1==zl)) && n%2==0 && n!=2 && n%4!=0 && (n/2)%2!=1) ans-=1+(nb==n); //~ cout<<ans<<"\n"; //~ cout<<"WTF "<<cw2<<" "<<zl2<<" "<<n<<"\n"; if (((cw2==cw && zl2==0) || (cw2==0 && zl2==zl)) && n%2!=0 && n!=1) ans-=1+(nb==n); if (ans<0) ans+=MOD; cout<<ans<<"\n"; } int main() { pd[0]=1; for (int i=1; i<MXN; i++) { pd[i]=(pd[i-1]<<1); if (pd[i]>=MOD) pd[i]-=MOD; } ios_base::sync_with_stdio(false); cin.tie(NULL); int n, q; cin>>n>>q; a.resize(n+1); for (int i=1; i<=n; i++) { cin>>a[i]; cnt[a[i]][i%4]++; } solve(); for (int i=0; i<q; i++) { int p; cin>>p; cnt[a[p]][p%4]--; cin>>a[p]; cnt[a[p]][p%4]++; solve(); } 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 | #include <bits/stdc++.h> using namespace std; const int MOD=1e9+7, MXN=2e5+1; const long long INVT=333333336; long long pd[MXN]; int cnt[300][4], rep[]={1, 2, 1, -1, -2, -1}; long long bs(int n, int x) { if (x==1) { long long r=(pd[n]-rep[(n+2)%6])*INVT%MOD; if (r<0) r+=MOD; return r; } else if (x==2) { long long r=(pd[n]-rep[n%6])*INVT%MOD; if (r<0) r+=MOD; return r; } else //if(x==0) { long long r=((pd[n]+rep[(n+1)%6])*INVT%MOD); if (r<0) r+=MOD; return r; } } vector<char>a; void solve() { int cw=cnt['C'][0]+cnt['C'][1]+cnt['C'][2]+cnt['C'][3]; int zl=cnt['Z'][0]+cnt['Z'][1]+cnt['Z'][2]+cnt['Z'][3]; int nb=cnt['N'][0]+cnt['N'][1]+cnt['N'][2]+cnt['N'][3]; int x=(cw+2*zl+2*nb)%3, n=cw+zl+nb; //~ cout<<cw<<" "<<zl<<" "<<nb<<" "<<x<<"\n"; long long ans=bs(nb, (x+1)%3)+bs(nb, (x+2)%3); //~ cout<<bs(nb, (x+1)%3)<<" "<<bs(nb, (x+2)%3)<<"\n"; int cw1=cnt['C'][0]+cnt['C'][1], cw2=cnt['C'][0]+cnt['C'][2]; int zl1=cnt['Z'][0]+cnt['Z'][1], zl2=cnt['Z'][0]+cnt['Z'][2]; if (((cw1==cw && zl1==0) || (cw1==0 && zl1==zl)) && n%2==0 && n!=2 && n%4!=0 && (n/2)%2!=1) ans-=1+(nb==n); //~ cout<<ans<<"\n"; //~ cout<<"WTF "<<cw2<<" "<<zl2<<" "<<n<<"\n"; if (((cw2==cw && zl2==0) || (cw2==0 && zl2==zl)) && n%2!=0 && n!=1) ans-=1+(nb==n); if (ans<0) ans+=MOD; cout<<ans<<"\n"; } int main() { pd[0]=1; for (int i=1; i<MXN; i++) { pd[i]=(pd[i-1]<<1); if (pd[i]>=MOD) pd[i]-=MOD; } ios_base::sync_with_stdio(false); cin.tie(NULL); int n, q; cin>>n>>q; a.resize(n+1); for (int i=1; i<=n; i++) { cin>>a[i]; cnt[a[i]][i%4]++; } solve(); for (int i=0; i<q; i++) { int p; cin>>p; cnt[a[p]][p%4]--; cin>>a[p]; cnt[a[p]][p%4]++; solve(); } return 0; } |