#include <bits/stdc++.h> #define ll long long #define fors(u, n, s) for(ll u=(s); u < (n); u++) #define foru(u, n) fors(u, n, 0) #define ir(a, b, x) (((a) <= (x)) && ((x) <= (b))) using namespace std; #define N 200100 #define M 1000000007LL #define INV_3 333333336LL ll n; char arr[N]; ll wildcards; ll c; ll z; ll c_alt; ll z_alt; void setup(){ wildcards=0; c=0; z=0; c_alt=0; z_alt=0; foru(i, n){ wildcards+=(arr[i]=='N'); c+=(arr[i]=='C'); z+=(arr[i]=='Z'); if((i&1)==0){ c_alt += (arr[i]=='Z'); z_alt += (arr[i]=='C'); }else{ c_alt += (arr[i]=='C'); z_alt += (arr[i]=='Z'); } } } void update(){ ll x; char chr; cin >> x >> chr; x--; wildcards-=(arr[x]=='N'); c-=(arr[x]=='C'); z-=(arr[x]=='Z'); if((x&1)==0){ c_alt -= (arr[x]=='Z'); z_alt -= (arr[x]=='C'); }else{ c_alt -= (arr[x]=='C'); z_alt -= (arr[x]=='Z'); } arr[x]=chr; wildcards+=(arr[x]=='N'); c+=(arr[x]=='C'); z+=(arr[x]=='Z'); if((x&1)==0){ c_alt += (arr[x]=='Z'); z_alt += (arr[x]=='C'); }else{ c_alt += (arr[x]=='C'); z_alt += (arr[x]=='Z'); } } ll fast_exp(ll a, int b){ ll ans = 1; while(b!=0){ if(b&1) {b--; ans=(ans*a)%M;} a=(a*a)%M; b=b/2; } return ans; } ll cnt(){ if(n==1) return 1 + (arr[0]=='N'); ll ans = fast_exp(2, wildcards); if((2*c+z)%3==0){ ll x = fast_exp(2, wildcards); if(wildcards&1) x-=2; else x+=2; x=x%M; x=(x*INV_3)%M; ans-=x; ans+=M; ans=ans%M; }else{ ll x = fast_exp(2, wildcards); if(wildcards&1) x+=1; else x-=1; x=x%M; x=(x*INV_3)%M; ans-=x; ans+=M; ans=ans%M; } if(c_alt==0){ if((n&1)!=0) ans--; } if(z_alt==0){ if((n&1)!=0) ans--; } ans+=M; ans=ans%M; return ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int q; cin >> n >> q; foru(i, n) cin >> arr[i]; setup(); cout << cnt() << endl; foru(i, q){ update(); cout << cnt() << endl; } 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 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 | #include <bits/stdc++.h> #define ll long long #define fors(u, n, s) for(ll u=(s); u < (n); u++) #define foru(u, n) fors(u, n, 0) #define ir(a, b, x) (((a) <= (x)) && ((x) <= (b))) using namespace std; #define N 200100 #define M 1000000007LL #define INV_3 333333336LL ll n; char arr[N]; ll wildcards; ll c; ll z; ll c_alt; ll z_alt; void setup(){ wildcards=0; c=0; z=0; c_alt=0; z_alt=0; foru(i, n){ wildcards+=(arr[i]=='N'); c+=(arr[i]=='C'); z+=(arr[i]=='Z'); if((i&1)==0){ c_alt += (arr[i]=='Z'); z_alt += (arr[i]=='C'); }else{ c_alt += (arr[i]=='C'); z_alt += (arr[i]=='Z'); } } } void update(){ ll x; char chr; cin >> x >> chr; x--; wildcards-=(arr[x]=='N'); c-=(arr[x]=='C'); z-=(arr[x]=='Z'); if((x&1)==0){ c_alt -= (arr[x]=='Z'); z_alt -= (arr[x]=='C'); }else{ c_alt -= (arr[x]=='C'); z_alt -= (arr[x]=='Z'); } arr[x]=chr; wildcards+=(arr[x]=='N'); c+=(arr[x]=='C'); z+=(arr[x]=='Z'); if((x&1)==0){ c_alt += (arr[x]=='Z'); z_alt += (arr[x]=='C'); }else{ c_alt += (arr[x]=='C'); z_alt += (arr[x]=='Z'); } } ll fast_exp(ll a, int b){ ll ans = 1; while(b!=0){ if(b&1) {b--; ans=(ans*a)%M;} a=(a*a)%M; b=b/2; } return ans; } ll cnt(){ if(n==1) return 1 + (arr[0]=='N'); ll ans = fast_exp(2, wildcards); if((2*c+z)%3==0){ ll x = fast_exp(2, wildcards); if(wildcards&1) x-=2; else x+=2; x=x%M; x=(x*INV_3)%M; ans-=x; ans+=M; ans=ans%M; }else{ ll x = fast_exp(2, wildcards); if(wildcards&1) x+=1; else x-=1; x=x%M; x=(x*INV_3)%M; ans-=x; ans+=M; ans=ans%M; } if(c_alt==0){ if((n&1)!=0) ans--; } if(z_alt==0){ if((n&1)!=0) ans--; } ans+=M; ans=ans%M; return ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int q; cin >> n >> q; foru(i, n) cin >> arr[i]; setup(); cout << cnt() << endl; foru(i, q){ update(); cout << cnt() << endl; } return 0; } |