#include<bits/stdc++.h> #define FOR(i,a,b) for(int i=a;i<(int)b;++i) #define FORD(i,a,b) for(int i=a;i>=(int)b;--i) #define PB push_back #define EB emplace_back #define FI first #define SE second #define umap unordered_map #define uset unordered_set #define vi vector<int> #define vvi vector<vi> #define vll vector<ll> #define vvll vector<vll> #define vpii vector<pii> #define pii pair<int, int> #define pll pair<ll, ll> #define ALL(X) (X).begin(),(X).end() #ifndef DEBUG #define endl (char)10 #endif using namespace std; using ll = long long; using ld = long double; template <class T> istream& operator>> (istream& is, vector<T>& vec){ FOR(i,0,vec.size()) is >> vec[i]; return is; } template <class T> ostream& operator<< (ostream& os, vector<T>& vec){ for(auto& t : vec) os << t << " "; return os; } template<class T, class U> ostream& operator<< (ostream& os, const pair<T, U>& p){ os << p.FI << " " << p.SE; return os; } template<class T, class U> istream& operator>> (istream& is, pair<T, U>& p){ is >> p.FI >> p.SE; return is; } template<const int MOD> struct MintBase { static const int P = MOD; ll x; MintBase<MOD>() : x(0){} MintBase<MOD>(ll x_) : x(normalize(x_)) {} MintBase<MOD>(const MintBase<MOD>& o) : x(o.x) {}; static ll normalize(ll x_) { x_ %= MOD; if (x_ < 0) x_ += MOD; return x_; } bool operator==(const MintBase<MOD>& o) const { return x == o.x; } MintBase<MOD> operator+ (const MintBase<MOD>& o) const { MintBase<MOD> res(*this); res += o; return res; } MintBase<MOD> operator- (const MintBase<MOD>& o) const { MintBase<MOD> res(*this); res -= o; return res; } MintBase<MOD>& operator+= (const MintBase<MOD>& o) { x += o.x; if (x >= MOD) x -= MOD; return *this; } MintBase<MOD>& operator++() { x += 1; if (x == MOD) x = 0; return *this; } MintBase<MOD>& operator--() { if (x == 0) x = MOD - 1; else x--; return *this; } MintBase<MOD>& operator-= (const MintBase<MOD>& o) { x -= o.x; if (x < 0) x += MOD; return *this; } MintBase<MOD> operator* (const MintBase<MOD>& o) const { return MintBase<MOD>(x * o.x); } MintBase<MOD>& operator*= (const MintBase<MOD>& o) { x *= o.x; x %= MOD; return *this; } MintBase<MOD> operator/ (const MintBase<MOD>& o) const { return (*this) * (o.inv()); } MintBase<MOD> inv() const { // assuming MOD is prime return pow(MOD - 2); } MintBase<MOD> pow(int k) const { MintBase<MOD> ans = 1; MintBase<MOD> pom = *this; while(k > 0){ if (k % 2 == 1) ans = ans * pom; pom = pom * pom; k /= 2; } return ans; } }; template<const int MOD> ostream& operator<< (ostream& os, const MintBase<MOD>& mint){ return os << mint.x; } template<const int MOD> istream& operator>> (istream& is, MintBase<MOD>& mint){ return (is >> mint.x); } using Mint = MintBase<1'000'000'007>; //using Mint = MintBase<998'244'353>; using vmi = vector<Mint>; int main () { ios_base::sync_with_stdio(false); cin.tie(0); int n, q; string s; cin >> n >> q >> s; vi B(n); int popcnt = 0; int pytcnt = 0; vvi loks(2, vi(2, 0)); FOR(i,0,n){ if (s[i] == 'Z'){ B[i] = 0; loks[0][i % 2]++; } if (s[i] == 'C'){ B[i] = 1; popcnt++; loks[1][i % 2]++; } if (s[i] == 'N'){ B[i] = -1; pytcnt++; } } vector<vmi> P(n + 1, vmi(3, 0)); P[0][0] = 1; FOR(i,1,n+1){ FOR(j,0,3){ FOR(k,0,3){ if (j != (k + 1) % 3) P[i][k] += P[i - 1][j]; } } } FOR(i,0,q+1){ Mint ans = 0; FOR(j,0,3) if ((n + popcnt + j) % 3 != 0) ans += P[pytcnt][j]; //cout << B << ": " << popcnt << " " << ans << endl; if (n % 2 == 1){ if (loks[0][0] == 0 && loks[1][1] == 0) --ans; if (loks[0][1] == 0 && loks[1][0] == 0) --ans; } cout << ans << endl; if (i != q){ int idx; char c; cin >> idx >> c; idx--; if (B[idx] == -1){ pytcnt--; } else if (B[idx] == 0){ loks[0][idx % 2]--; } else { loks[1][idx % 2]--; popcnt--; } if (c == 'N'){ B[idx] = -1; pytcnt++; } else if (c == 'Z'){ B[idx] = 0; loks[0][idx % 2]++; } else { B[idx] = 1; loks[1][idx % 2]++; popcnt++; } } } }
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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | #include<bits/stdc++.h> #define FOR(i,a,b) for(int i=a;i<(int)b;++i) #define FORD(i,a,b) for(int i=a;i>=(int)b;--i) #define PB push_back #define EB emplace_back #define FI first #define SE second #define umap unordered_map #define uset unordered_set #define vi vector<int> #define vvi vector<vi> #define vll vector<ll> #define vvll vector<vll> #define vpii vector<pii> #define pii pair<int, int> #define pll pair<ll, ll> #define ALL(X) (X).begin(),(X).end() #ifndef DEBUG #define endl (char)10 #endif using namespace std; using ll = long long; using ld = long double; template <class T> istream& operator>> (istream& is, vector<T>& vec){ FOR(i,0,vec.size()) is >> vec[i]; return is; } template <class T> ostream& operator<< (ostream& os, vector<T>& vec){ for(auto& t : vec) os << t << " "; return os; } template<class T, class U> ostream& operator<< (ostream& os, const pair<T, U>& p){ os << p.FI << " " << p.SE; return os; } template<class T, class U> istream& operator>> (istream& is, pair<T, U>& p){ is >> p.FI >> p.SE; return is; } template<const int MOD> struct MintBase { static const int P = MOD; ll x; MintBase<MOD>() : x(0){} MintBase<MOD>(ll x_) : x(normalize(x_)) {} MintBase<MOD>(const MintBase<MOD>& o) : x(o.x) {}; static ll normalize(ll x_) { x_ %= MOD; if (x_ < 0) x_ += MOD; return x_; } bool operator==(const MintBase<MOD>& o) const { return x == o.x; } MintBase<MOD> operator+ (const MintBase<MOD>& o) const { MintBase<MOD> res(*this); res += o; return res; } MintBase<MOD> operator- (const MintBase<MOD>& o) const { MintBase<MOD> res(*this); res -= o; return res; } MintBase<MOD>& operator+= (const MintBase<MOD>& o) { x += o.x; if (x >= MOD) x -= MOD; return *this; } MintBase<MOD>& operator++() { x += 1; if (x == MOD) x = 0; return *this; } MintBase<MOD>& operator--() { if (x == 0) x = MOD - 1; else x--; return *this; } MintBase<MOD>& operator-= (const MintBase<MOD>& o) { x -= o.x; if (x < 0) x += MOD; return *this; } MintBase<MOD> operator* (const MintBase<MOD>& o) const { return MintBase<MOD>(x * o.x); } MintBase<MOD>& operator*= (const MintBase<MOD>& o) { x *= o.x; x %= MOD; return *this; } MintBase<MOD> operator/ (const MintBase<MOD>& o) const { return (*this) * (o.inv()); } MintBase<MOD> inv() const { // assuming MOD is prime return pow(MOD - 2); } MintBase<MOD> pow(int k) const { MintBase<MOD> ans = 1; MintBase<MOD> pom = *this; while(k > 0){ if (k % 2 == 1) ans = ans * pom; pom = pom * pom; k /= 2; } return ans; } }; template<const int MOD> ostream& operator<< (ostream& os, const MintBase<MOD>& mint){ return os << mint.x; } template<const int MOD> istream& operator>> (istream& is, MintBase<MOD>& mint){ return (is >> mint.x); } using Mint = MintBase<1'000'000'007>; //using Mint = MintBase<998'244'353>; using vmi = vector<Mint>; int main () { ios_base::sync_with_stdio(false); cin.tie(0); int n, q; string s; cin >> n >> q >> s; vi B(n); int popcnt = 0; int pytcnt = 0; vvi loks(2, vi(2, 0)); FOR(i,0,n){ if (s[i] == 'Z'){ B[i] = 0; loks[0][i % 2]++; } if (s[i] == 'C'){ B[i] = 1; popcnt++; loks[1][i % 2]++; } if (s[i] == 'N'){ B[i] = -1; pytcnt++; } } vector<vmi> P(n + 1, vmi(3, 0)); P[0][0] = 1; FOR(i,1,n+1){ FOR(j,0,3){ FOR(k,0,3){ if (j != (k + 1) % 3) P[i][k] += P[i - 1][j]; } } } FOR(i,0,q+1){ Mint ans = 0; FOR(j,0,3) if ((n + popcnt + j) % 3 != 0) ans += P[pytcnt][j]; //cout << B << ": " << popcnt << " " << ans << endl; if (n % 2 == 1){ if (loks[0][0] == 0 && loks[1][1] == 0) --ans; if (loks[0][1] == 0 && loks[1][0] == 0) --ans; } cout << ans << endl; if (i != q){ int idx; char c; cin >> idx >> c; idx--; if (B[idx] == -1){ pytcnt--; } else if (B[idx] == 0){ loks[0][idx % 2]--; } else { loks[1][idx % 2]--; popcnt--; } if (c == 'N'){ B[idx] = -1; pytcnt++; } else if (c == 'Z'){ B[idx] = 0; loks[0][idx % 2]++; } else { B[idx] = 1; loks[1][idx % 2]++; popcnt++; } } } } |