#include <bits/stdc++.h> #define zesp complex<long double> using namespace std; const long long mod=1e9+7; const long long mod_inv=333333336; const int maxn=200003; class eisen_mod { public: long long re, im; eisen_mod(long long a, long long b=0) { re = a%mod; if(re<0) re+=mod; im = b%mod; if(im<0) im+=mod; } eisen_mod operator+(const eisen_mod z) {return eisen_mod(re+z.re, im+z.im);} eisen_mod operator-(const eisen_mod z) {return eisen_mod(re-z.re, im-z.im);} eisen_mod operator*(const eisen_mod z) {return eisen_mod(re*z.re-im*z.im, im*z.re+re*z.im-im*z.im);} }; eisen_mod omega(0,1); eisen_mod sp(eisen_mod a, int n) { if(n==0) return eisen_mod(1); eisen_mod pom=sp(a,n/2); if(n%2 == 0) return pom*pom; return pom*pom*a; } long long root_filter(int m, int n) { m%=3; if(m<0) m+=3; if(m == 0) { return ((sp(eisen_mod(2,0),n) + sp(eisen_mod(1,1),n) + sp(eisen_mod(0,-1),n)) * eisen_mod(mod_inv)).re; } if(m == 1) { return ((sp(eisen_mod(2,0),n) + omega*omega*sp(eisen_mod(1,1),n) + omega*sp(eisen_mod(0,-1),n)) * eisen_mod(mod_inv)).re; } if(m == 2) { return ((sp(eisen_mod(2,0),n) + omega*sp(eisen_mod(1,1),n) + omega*omega*sp(eisen_mod(0,-1),n)) * eisen_mod(mod_inv)).re; } } long long wzorek(int C, int Z, int B, int deviation, int antideviation, int n) { //cout<<C<<" "<<Z<<" "<<deviation<<" "<<antideviation<<" "<<n<<"\n"; int m = 2*(B-Z+C); long long odp = root_filter(m+1, B)+root_filter(m+2, B); if((n%2 == 1)&&(deviation == n)) odp--; if((n%2 == 1)&&(antideviation == n)) odp--; odp%=mod; if(odp<0) odp+=mod; return odp; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); int n,q; cin>>n>>q; string s; cin>>s; if(n==1) { if(s[0]=='N') cout<<"2\n"; else cout<<"1\n"; while(q--) { int x; char zn; cin>>x>>zn; s[0]=zn; if(s[0]=='N') cout<<"2\n"; else cout<<"1\n"; } } else { int C=0,Z=0,B=0; int deviation=0; int antideviation=0; bitset<maxn> c,nb; for(int i=0;i<n;i++) { if(s[i]=='C') { c[i]=1; nb[i]=1; C++; } if(s[i]=='Z') { c[i]=0; nb[i]=1; Z++; } if(s[i]=='N') { c[i]=0; nb[i]=0; B++; } if((c[i] == (i%2)) || (nb[i] == 0)) deviation++; if((c[i] != (i%2)) || (nb[i] == 0)) antideviation++; } cout<<wzorek(C, Z, B, deviation, antideviation, n)<<"\n"; int x; char zn; while(q--) { cin>>x>>zn; x--; if((c[x] == (x%2)) || (nb[x] == 0)) deviation--; if((c[x] != (x%2)) || (nb[x] == 0)) antideviation--; if((c[x] == 0)&&(nb[x]==1)) Z--; if(c[x] == 1) C--; if(nb[x]==0) B--; if(zn == 'Z') { c[x]=0; nb[x]=1; Z++; } if(zn == 'C') { c[x]=1; nb[x]=1; C++; } if(zn == 'N') { c[x]=0; nb[x]=0; B++; } if((c[x] == (x%2)) || (nb[x] == 0)) deviation++; if((c[x] != (x%2)) || (nb[x] == 0)) antideviation++; cout<<wzorek(C, Z, B, deviation, antideviation, n)<<"\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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | #include <bits/stdc++.h> #define zesp complex<long double> using namespace std; const long long mod=1e9+7; const long long mod_inv=333333336; const int maxn=200003; class eisen_mod { public: long long re, im; eisen_mod(long long a, long long b=0) { re = a%mod; if(re<0) re+=mod; im = b%mod; if(im<0) im+=mod; } eisen_mod operator+(const eisen_mod z) {return eisen_mod(re+z.re, im+z.im);} eisen_mod operator-(const eisen_mod z) {return eisen_mod(re-z.re, im-z.im);} eisen_mod operator*(const eisen_mod z) {return eisen_mod(re*z.re-im*z.im, im*z.re+re*z.im-im*z.im);} }; eisen_mod omega(0,1); eisen_mod sp(eisen_mod a, int n) { if(n==0) return eisen_mod(1); eisen_mod pom=sp(a,n/2); if(n%2 == 0) return pom*pom; return pom*pom*a; } long long root_filter(int m, int n) { m%=3; if(m<0) m+=3; if(m == 0) { return ((sp(eisen_mod(2,0),n) + sp(eisen_mod(1,1),n) + sp(eisen_mod(0,-1),n)) * eisen_mod(mod_inv)).re; } if(m == 1) { return ((sp(eisen_mod(2,0),n) + omega*omega*sp(eisen_mod(1,1),n) + omega*sp(eisen_mod(0,-1),n)) * eisen_mod(mod_inv)).re; } if(m == 2) { return ((sp(eisen_mod(2,0),n) + omega*sp(eisen_mod(1,1),n) + omega*omega*sp(eisen_mod(0,-1),n)) * eisen_mod(mod_inv)).re; } } long long wzorek(int C, int Z, int B, int deviation, int antideviation, int n) { //cout<<C<<" "<<Z<<" "<<deviation<<" "<<antideviation<<" "<<n<<"\n"; int m = 2*(B-Z+C); long long odp = root_filter(m+1, B)+root_filter(m+2, B); if((n%2 == 1)&&(deviation == n)) odp--; if((n%2 == 1)&&(antideviation == n)) odp--; odp%=mod; if(odp<0) odp+=mod; return odp; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); int n,q; cin>>n>>q; string s; cin>>s; if(n==1) { if(s[0]=='N') cout<<"2\n"; else cout<<"1\n"; while(q--) { int x; char zn; cin>>x>>zn; s[0]=zn; if(s[0]=='N') cout<<"2\n"; else cout<<"1\n"; } } else { int C=0,Z=0,B=0; int deviation=0; int antideviation=0; bitset<maxn> c,nb; for(int i=0;i<n;i++) { if(s[i]=='C') { c[i]=1; nb[i]=1; C++; } if(s[i]=='Z') { c[i]=0; nb[i]=1; Z++; } if(s[i]=='N') { c[i]=0; nb[i]=0; B++; } if((c[i] == (i%2)) || (nb[i] == 0)) deviation++; if((c[i] != (i%2)) || (nb[i] == 0)) antideviation++; } cout<<wzorek(C, Z, B, deviation, antideviation, n)<<"\n"; int x; char zn; while(q--) { cin>>x>>zn; x--; if((c[x] == (x%2)) || (nb[x] == 0)) deviation--; if((c[x] != (x%2)) || (nb[x] == 0)) antideviation--; if((c[x] == 0)&&(nb[x]==1)) Z--; if(c[x] == 1) C--; if(nb[x]==0) B--; if(zn == 'Z') { c[x]=0; nb[x]=1; Z++; } if(zn == 'C') { c[x]=1; nb[x]=1; C++; } if(zn == 'N') { c[x]=0; nb[x]=0; B++; } if((c[x] == (x%2)) || (nb[x] == 0)) deviation++; if((c[x] != (x%2)) || (nb[x] == 0)) antideviation++; cout<<wzorek(C, Z, B, deviation, antideviation, n)<<"\n"; } } } |