#include <bits/stdc++.h> using namespace std; #define PB push_back #define LL long long #define int LL #define FOR(i,a,b) for (int i = (a); i <= (b); i++) #define FORD(i,a,b) for (int i = (a); i >= (b); i--) #define REP(i,n) FOR(i,0,(int)(n)-1) #define st first #define nd second #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #define VI vector<int> #define PII pair<int,int> #define LD long double template<class T> ostream &operator<<(ostream &os, vector<T> V){ os<<"[";for(auto vv:V)os<<vv<<",";return os<<"]"; } template<class T, size_t N> ostream &operator<<(ostream &os, array<T, N> V){ os<<"[";for(auto vv:V)os<<vv<<",";return os<<"]"; } template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P) { return os << "(" << P.st << "," << P.nd << ")"; } template<class C> void mini(C& a4, C b4) { a4 = min(a4, b4); } template<class C> void maxi(C& a4, C b4) { a4 = max(a4, b4); } template<class TH> void _dbg(const char *sdbg, TH h){cerr<<sdbg<<"="<<h<<"\n";} template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while(*sdbg!=',')cerr<<*sdbg++;cerr<<"="<<h<<","; _dbg(sdbg+1, a...); } #ifdef LOCAL #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) (__VA_ARGS__) #define cerr if(0)cout #endif int n,q; string s; vector<int> cnt(3); vector<int> matching(2); const int mod = 1e9 + 7; bool match(char c, int idx, bool option){ switch(c){ case 'N': return true; case 'C': return option ^ (idx & 1); case 'Z': return !option ^ (idx & 1); default: assert(false); } } int char2int(char c){ switch(c){ case 'N': return 0; case 'C': return 1; case 'Z': return 2; default: assert(false); } } char int2char(int idx){ switch(idx){ case 0: return 'N'; case 1: return 'C'; case 2: return 'Z'; default: assert(false); } } void update(int idx, int diff){ REP(j, 2){ matching[j] += diff * match(s[idx], idx, j); } cnt[char2int(s[idx])] += diff; } // 3x3 array typedef array<array<int, 3>, 3> State; State get_basic(char c){ State result; switch(c){ case 'N': result = {{{0, 1, 1}, {1, 0, 1}, {1, 1, 0}}}; break; case 'C': result = {{{0, 0, 1}, {1, 0, 0}, {0, 1, 0}}}; break; case 'Z': result = {{{0, 1, 0}, {0, 0, 1}, {1, 0, 0}}}; break; default: assert(false); } return result; } // matrix multiply State State operator*(const State& a, const State& b){ State result; REP(i, 3){ REP(j, 3){ result[i][j] = 0; REP(k, 3){ result[i][j] += a[i][k] * b[k][j]; result[i][j] %= mod; } } } return result; } // *= operator State& operator*=(State& a, const State& b){ a = a * b; return a; } State power(State a, int p){ State result; REP(i, 3){ REP(j, 3){ result[i][j] = (i == j); } } while(p){ if(p & 1){ result = result * a; } a = a * a; p >>= 1; } return result; } vector<vector<State>> states(3, vector<State>(200 * 1000 + 1)); int get_ans() { State result = { {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}} }; REP(i, 3) { // result *= power(get_basic(int2char(i)), cnt[i]); result *= states[i][cnt[i]]; } int answer = result[0][1] + result[0][2]; answer %= mod; debug(matching); if (n & 1) answer -= (matching[0] == n) + (matching[1] == n); if (answer < 0) { answer += mod; } return answer; } void preproc(){ REP(i, 3){ State result = { {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}} }; states[i][0] = result; REP(j, 200 * 1000){ states[i][j + 1] = states[i][j] * get_basic(int2char(i));; } } }; int32_t main() { // initialize default random engine preproc(); default_random_engine gen; ios_base::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(11); cerr << fixed << setprecision(6); cin >> n >> q; cin >> s; REP(i, n){ update(i, 1); } cout << get_ans() << "\n"; REP(_, q){ int i; char c; cin >> i >> c; i--; update(i, -1); s[i] = c; update(i, 1); cout << get_ans() << "\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 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 214 215 216 217 218 | #include <bits/stdc++.h> using namespace std; #define PB push_back #define LL long long #define int LL #define FOR(i,a,b) for (int i = (a); i <= (b); i++) #define FORD(i,a,b) for (int i = (a); i >= (b); i--) #define REP(i,n) FOR(i,0,(int)(n)-1) #define st first #define nd second #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #define VI vector<int> #define PII pair<int,int> #define LD long double template<class T> ostream &operator<<(ostream &os, vector<T> V){ os<<"[";for(auto vv:V)os<<vv<<",";return os<<"]"; } template<class T, size_t N> ostream &operator<<(ostream &os, array<T, N> V){ os<<"[";for(auto vv:V)os<<vv<<",";return os<<"]"; } template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P) { return os << "(" << P.st << "," << P.nd << ")"; } template<class C> void mini(C& a4, C b4) { a4 = min(a4, b4); } template<class C> void maxi(C& a4, C b4) { a4 = max(a4, b4); } template<class TH> void _dbg(const char *sdbg, TH h){cerr<<sdbg<<"="<<h<<"\n";} template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while(*sdbg!=',')cerr<<*sdbg++;cerr<<"="<<h<<","; _dbg(sdbg+1, a...); } #ifdef LOCAL #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) (__VA_ARGS__) #define cerr if(0)cout #endif int n,q; string s; vector<int> cnt(3); vector<int> matching(2); const int mod = 1e9 + 7; bool match(char c, int idx, bool option){ switch(c){ case 'N': return true; case 'C': return option ^ (idx & 1); case 'Z': return !option ^ (idx & 1); default: assert(false); } } int char2int(char c){ switch(c){ case 'N': return 0; case 'C': return 1; case 'Z': return 2; default: assert(false); } } char int2char(int idx){ switch(idx){ case 0: return 'N'; case 1: return 'C'; case 2: return 'Z'; default: assert(false); } } void update(int idx, int diff){ REP(j, 2){ matching[j] += diff * match(s[idx], idx, j); } cnt[char2int(s[idx])] += diff; } // 3x3 array typedef array<array<int, 3>, 3> State; State get_basic(char c){ State result; switch(c){ case 'N': result = {{{0, 1, 1}, {1, 0, 1}, {1, 1, 0}}}; break; case 'C': result = {{{0, 0, 1}, {1, 0, 0}, {0, 1, 0}}}; break; case 'Z': result = {{{0, 1, 0}, {0, 0, 1}, {1, 0, 0}}}; break; default: assert(false); } return result; } // matrix multiply State State operator*(const State& a, const State& b){ State result; REP(i, 3){ REP(j, 3){ result[i][j] = 0; REP(k, 3){ result[i][j] += a[i][k] * b[k][j]; result[i][j] %= mod; } } } return result; } // *= operator State& operator*=(State& a, const State& b){ a = a * b; return a; } State power(State a, int p){ State result; REP(i, 3){ REP(j, 3){ result[i][j] = (i == j); } } while(p){ if(p & 1){ result = result * a; } a = a * a; p >>= 1; } return result; } vector<vector<State>> states(3, vector<State>(200 * 1000 + 1)); int get_ans() { State result = { {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}} }; REP(i, 3) { // result *= power(get_basic(int2char(i)), cnt[i]); result *= states[i][cnt[i]]; } int answer = result[0][1] + result[0][2]; answer %= mod; debug(matching); if (n & 1) answer -= (matching[0] == n) + (matching[1] == n); if (answer < 0) { answer += mod; } return answer; } void preproc(){ REP(i, 3){ State result = { {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}} }; states[i][0] = result; REP(j, 200 * 1000){ states[i][j + 1] = states[i][j] * get_basic(int2char(i));; } } }; int32_t main() { // initialize default random engine preproc(); default_random_engine gen; ios_base::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(11); cerr << fixed << setprecision(6); cin >> n >> q; cin >> s; REP(i, n){ update(i, 1); } cout << get_ans() << "\n"; REP(_, q){ int i; char c; cin >> i >> c; i--; update(i, -1); s[i] = c; update(i, 1); cout << get_ans() << "\n"; } } |