#include <bits/stdc++.h> using namespace std; using ll = long long; using db = double; using ull = unsigned long long; using pi = pair<int, int>; using pl = pair<ll, ll>; using vi = vector<int>; using vl = vector<ll>; using vpi = vector<pi>; using vpl = vector<pl>; #define pb push_back #define eb emplace_back #define mp make_pair #define f first #define s second #define tcT template<class T #define tcTU tcT, class U #define sz(x) int((x).size()) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define rep(i,a,b) for(int i = (a); i < (b); i++) #define rrep(i,a,b) for(int i = (b) - 1; i >= (a); i--) const db PI = acos(-1); mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count()); tcT> bool ckmin(T& a, const T& b) { return b < a ? a=b, 1 : 0; } tcT> bool ckmax(T& a, const T& b) { return a < b ? a=b, 1 : 0; } tcT> void _dbg(const char *sdbg, T h){ cerr<<sdbg<<'='<<h<<endl; } tcT, class... TA> void _dbg(const char *sdbg, T h, TA... a) { while(*sdbg!=',') cerr<<*sdbg++; cerr<<'='<<h<<','; _dbg(sdbg+1, a...); } tcT> ostream &operator<<(ostream& os, vector<T> V) { os << "["; for (auto vv : V) os << vv << ","; return os << "]"; } tcT, class V> ostream &operator<<(ostream &os, pair<T,V> P) { return os << "(" << P.f << "," << P.s << ")"; } #ifdef LOCAL #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) (__VA_ARGS__) #define cerr if(0)cout #endif constexpr int MAXN = 2e5+10; constexpr ll MOD = 1e9+7; constexpr ll inv2 = (MOD+1)/2; int n, q; string s; ll total = 1; int bad[2]; // 0 -> 01010, 1 -> 10101 int sc[256]; int a[MAXN]; int cnt2 = 0; ll how_many[MAXN][3]; int diff = 0; void preprocess() { sc['C'] = 0; sc['Z'] = 1; sc['N'] = 2; how_many[0][0] = 1; rep(i,1,MAXN) { rep(j,0,3) { how_many[i][(j+1)%3] += how_many[i-1][j]; how_many[i][(j+2)%3] += how_many[i-1][j]; } rep(j,0,3) { how_many[i][j] %= MOD; } } } ll calc_res() { ll res = total; res = (res + MOD - how_many[cnt2][diff]) % MOD; if (n%2 && n != 1 && bad[0] == 0) res = (res + MOD - 1) % MOD; if (n%2 && n != 1 && bad[1] == 0) res = (res + MOD - 1) % MOD; return res; } void upd(int i, int na) { if (a[i] == 2) { total = (total * inv2) % MOD; cnt2--; } else if (a[i] == 1) { bad[i&1]--; diff = (diff + 2) % 3; } else { bad[(i&1)^1]--; diff = (diff + 1) % 3; } a[i] = na; if (a[i] == 2) { total = (total * 2) % MOD; cnt2++; } else if (a[i] == 1) { bad[i&1]++; diff = (diff + 1) % 3; } else { bad[(i&1)^1]++; diff = (diff + 2) % 3; } } signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); preprocess(); cin >> n >> q >> s; rep(i,0,n) a[i] = sc[(int)s[i]]; rep(i,0,n) { if (a[i] == 2) { total = (total * 2) % MOD; cnt2++; } else if (a[i] == 1) { bad[i&1]++; diff = (diff + 1) % 3; } else { bad[(i&1)^1]++; diff = (diff + 2) % 3; } } cout << calc_res() << "\n"; rep(xdddd,0,q) { int i; char c; cin >> i >> c; i--; int x = sc[(int)c]; upd(i, x); cout << calc_res() << "\n"; } 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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | #include <bits/stdc++.h> using namespace std; using ll = long long; using db = double; using ull = unsigned long long; using pi = pair<int, int>; using pl = pair<ll, ll>; using vi = vector<int>; using vl = vector<ll>; using vpi = vector<pi>; using vpl = vector<pl>; #define pb push_back #define eb emplace_back #define mp make_pair #define f first #define s second #define tcT template<class T #define tcTU tcT, class U #define sz(x) int((x).size()) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define rep(i,a,b) for(int i = (a); i < (b); i++) #define rrep(i,a,b) for(int i = (b) - 1; i >= (a); i--) const db PI = acos(-1); mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count()); tcT> bool ckmin(T& a, const T& b) { return b < a ? a=b, 1 : 0; } tcT> bool ckmax(T& a, const T& b) { return a < b ? a=b, 1 : 0; } tcT> void _dbg(const char *sdbg, T h){ cerr<<sdbg<<'='<<h<<endl; } tcT, class... TA> void _dbg(const char *sdbg, T h, TA... a) { while(*sdbg!=',') cerr<<*sdbg++; cerr<<'='<<h<<','; _dbg(sdbg+1, a...); } tcT> ostream &operator<<(ostream& os, vector<T> V) { os << "["; for (auto vv : V) os << vv << ","; return os << "]"; } tcT, class V> ostream &operator<<(ostream &os, pair<T,V> P) { return os << "(" << P.f << "," << P.s << ")"; } #ifdef LOCAL #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) (__VA_ARGS__) #define cerr if(0)cout #endif constexpr int MAXN = 2e5+10; constexpr ll MOD = 1e9+7; constexpr ll inv2 = (MOD+1)/2; int n, q; string s; ll total = 1; int bad[2]; // 0 -> 01010, 1 -> 10101 int sc[256]; int a[MAXN]; int cnt2 = 0; ll how_many[MAXN][3]; int diff = 0; void preprocess() { sc['C'] = 0; sc['Z'] = 1; sc['N'] = 2; how_many[0][0] = 1; rep(i,1,MAXN) { rep(j,0,3) { how_many[i][(j+1)%3] += how_many[i-1][j]; how_many[i][(j+2)%3] += how_many[i-1][j]; } rep(j,0,3) { how_many[i][j] %= MOD; } } } ll calc_res() { ll res = total; res = (res + MOD - how_many[cnt2][diff]) % MOD; if (n%2 && n != 1 && bad[0] == 0) res = (res + MOD - 1) % MOD; if (n%2 && n != 1 && bad[1] == 0) res = (res + MOD - 1) % MOD; return res; } void upd(int i, int na) { if (a[i] == 2) { total = (total * inv2) % MOD; cnt2--; } else if (a[i] == 1) { bad[i&1]--; diff = (diff + 2) % 3; } else { bad[(i&1)^1]--; diff = (diff + 1) % 3; } a[i] = na; if (a[i] == 2) { total = (total * 2) % MOD; cnt2++; } else if (a[i] == 1) { bad[i&1]++; diff = (diff + 1) % 3; } else { bad[(i&1)^1]++; diff = (diff + 2) % 3; } } signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); preprocess(); cin >> n >> q >> s; rep(i,0,n) a[i] = sc[(int)s[i]]; rep(i,0,n) { if (a[i] == 2) { total = (total * 2) % MOD; cnt2++; } else if (a[i] == 1) { bad[i&1]++; diff = (diff + 1) % 3; } else { bad[(i&1)^1]++; diff = (diff + 2) % 3; } } cout << calc_res() << "\n"; rep(xdddd,0,q) { int i; char c; cin >> i >> c; i--; int x = sc[(int)c]; upd(i, x); cout << calc_res() << "\n"; } return 0; } |