// Author: Kajetan Ramsza #include "bits/stdc++.h" using namespace std; template<typename F, typename S> ostream& operator<<(ostream& os, const pair<F, S> &p) { return os<<"("<<p.first<<", "<<p.second<<")"; } template<typename T> ostream &operator<<(ostream & os, const vector<T> &v) { os << "{"; typename vector< T > :: const_iterator it; for( it = v.begin(); it != v.end(); it++ ) { if( it != v.begin() ) os << ", "; os << *it; } return os << "}"; } void dbg_out() { cerr<<'\n'; } template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr<<' '<<H; dbg_out(T...); } #ifdef DEBUG #define dbg(...) cerr<<"(" << #__VA_ARGS__ <<"):", dbg_out(__VA_ARGS__) #else #define dbg(...) #endif typedef long long ll; const ll mod = 1e9 + 7; ll inv_3; ll power(ll x, ll exp) { ll res = 1; while(exp > 0) { if(exp & 1) { res = res * x % mod; } x = x * x % mod; exp /= 2; } return res; } const vector<vector<ll>> add = { {1, 0, 0}, {1, 1, 0}, {0, 1, 0}, {0, 1, 1}, {0, 0, 1}, {1, 0 ,1} }; ll calc(ll n, ll m) { ll result = power(2, n); dbg(n, m); dbg(result); if(n % 2 == 0) result = (result + mod - 1) % mod; else result = (result + mod - 2) % mod; result = inv_3 * result % mod; dbg(result); result += add[n%6][m]; result %= mod; return result; } ll n, q; ll match[2]; ll blue = 0; ll lit = 0; ll res() { if(n == 1) { if(blue == 1) return 2; else return 1; } dbg(blue, lit, match[0], match[1]); // 2^blue - calc(blue, (6 - n%3 - lit%3) % 3) - match? ll result = power(2, blue); dbg(result); ll c = calc(blue, (6 - n%3 - lit%3) % 3); dbg(c); result = (result + mod - c) % mod; dbg(result); if(n % 2 != 0) { if(match[0] + blue == n) result = (mod + result - 1) % mod; if(match[1] + blue == n) result = (mod + result - 1) % mod; } dbg(result); return result; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); inv_3 = power(3, mod - 2); dbg(inv_3, 3 * inv_3 % mod); cin>>n>>q; vector<ll> vec(n); for(ll i=0;i<n;i++) { char c; cin>>c; if(c == 'N') { vec[i] = -1; blue++; } else if(c == 'Z') { vec[i] = 1; lit++; } else vec[i] = 0; } for(ll i=0;i<n;i++) { if(vec[i] == 0) { match[i%2]++; } else if(vec[i] == 1) { match[(i+1)%2]++; } } cout<<res()<<'\n'; for(ll i=0;i<q;i++) { int ind; char c; cin>>ind>>c; ind--; if(vec[ind] == -1) blue--; else { match[(ind + vec[ind])%2]--; if(vec[ind] == 1) lit--; } if(c == 'N') { vec[ind] = -1; blue++; } else if(c == 'Z') { vec[ind] = 1; lit++; match[(ind+1)%2]++; } else { vec[ind] = 0; match[ind%2]++; } cout<<res()<<'\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 | // Author: Kajetan Ramsza #include "bits/stdc++.h" using namespace std; template<typename F, typename S> ostream& operator<<(ostream& os, const pair<F, S> &p) { return os<<"("<<p.first<<", "<<p.second<<")"; } template<typename T> ostream &operator<<(ostream & os, const vector<T> &v) { os << "{"; typename vector< T > :: const_iterator it; for( it = v.begin(); it != v.end(); it++ ) { if( it != v.begin() ) os << ", "; os << *it; } return os << "}"; } void dbg_out() { cerr<<'\n'; } template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr<<' '<<H; dbg_out(T...); } #ifdef DEBUG #define dbg(...) cerr<<"(" << #__VA_ARGS__ <<"):", dbg_out(__VA_ARGS__) #else #define dbg(...) #endif typedef long long ll; const ll mod = 1e9 + 7; ll inv_3; ll power(ll x, ll exp) { ll res = 1; while(exp > 0) { if(exp & 1) { res = res * x % mod; } x = x * x % mod; exp /= 2; } return res; } const vector<vector<ll>> add = { {1, 0, 0}, {1, 1, 0}, {0, 1, 0}, {0, 1, 1}, {0, 0, 1}, {1, 0 ,1} }; ll calc(ll n, ll m) { ll result = power(2, n); dbg(n, m); dbg(result); if(n % 2 == 0) result = (result + mod - 1) % mod; else result = (result + mod - 2) % mod; result = inv_3 * result % mod; dbg(result); result += add[n%6][m]; result %= mod; return result; } ll n, q; ll match[2]; ll blue = 0; ll lit = 0; ll res() { if(n == 1) { if(blue == 1) return 2; else return 1; } dbg(blue, lit, match[0], match[1]); // 2^blue - calc(blue, (6 - n%3 - lit%3) % 3) - match? ll result = power(2, blue); dbg(result); ll c = calc(blue, (6 - n%3 - lit%3) % 3); dbg(c); result = (result + mod - c) % mod; dbg(result); if(n % 2 != 0) { if(match[0] + blue == n) result = (mod + result - 1) % mod; if(match[1] + blue == n) result = (mod + result - 1) % mod; } dbg(result); return result; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); inv_3 = power(3, mod - 2); dbg(inv_3, 3 * inv_3 % mod); cin>>n>>q; vector<ll> vec(n); for(ll i=0;i<n;i++) { char c; cin>>c; if(c == 'N') { vec[i] = -1; blue++; } else if(c == 'Z') { vec[i] = 1; lit++; } else vec[i] = 0; } for(ll i=0;i<n;i++) { if(vec[i] == 0) { match[i%2]++; } else if(vec[i] == 1) { match[(i+1)%2]++; } } cout<<res()<<'\n'; for(ll i=0;i<q;i++) { int ind; char c; cin>>ind>>c; ind--; if(vec[ind] == -1) blue--; else { match[(ind + vec[ind])%2]--; if(vec[ind] == 1) lit--; } if(c == 'N') { vec[ind] = -1; blue++; } else if(c == 'Z') { vec[ind] = 1; lit++; match[(ind+1)%2]++; } else { vec[ind] = 0; match[ind%2]++; } cout<<res()<<'\n'; } } |