//Franciszek Witt #include<bits/stdc++.h> using namespace std; #define FOR(i,l,r) for(int i=(l);i<=(r);++i) #define REP(i,n) FOR(i,0,(n)-1) #define ssize(x) int(x.size()) template<class A,class B>auto&operator<<(ostream&o,pair<A,B>p){return o<<'('<<p.first<<", "<<p.second<<')';} template<class T>auto operator<<(ostream&o,T x)->decltype(x.end(),o){o<<'{';int i=0;for(auto e:x)o<<(", ")+2*!i++<<e;return o<<'}';} #ifdef DEBUG #define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...);}(x),cerr<<'\n' #else #define debug(...) {} #endif typedef long long ll; const int mod = int(1e9) + 7; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, q; cin >> n >> q; auto nrm = [&] (int x) { x %= 3; if(x < 0) x += 3; return x; }; vector re(3, vector(n + 1, 0LL)); re[0][0] = 1LL; FOR(i, 1, n) REP(j, 3) re[j][i] = (re[j][i - 1] + re[nrm(j - 1)][i - 1]) % mod; int miss0 = 0, miss1 = 0, ze = 0, je = 0, idk = 0; auto upd = [&] (int x, int y, int sig) { if(y == -1) { idk += sig; return; } if((x + y) & 1) miss0 += sig; else miss1 += sig; if(y == 0) ze += sig; if(y == 1) je += sig; }; vector<int> ve; { string s; cin >> s; REP(i, n) { if(s[i] == 'C') { ve.emplace_back(0); } else if(s[i] == 'Z') { ve.emplace_back(1); } else { ve.emplace_back(-1); } upd(i, ve.back(), 1); } } auto gw = [&] () { ll wy = 0; auto prmiss = [&] (int ban) { if(n == 1) return; if((n / 2) % 3 != ban) wy -= !miss0; if(((n + 1) / 2) % 3 != ban) wy -= !miss1; }; if(n % 3 == 0) { wy = re[nrm(1 - je)][idk] + re[nrm(2 - je)][idk]; prmiss(0); } else if(n % 3 == 1) { wy = re[nrm(0 - je)][idk] + re[nrm(1 - je)][idk]; prmiss(2); } else if(n % 3 == 2) { wy = re[nrm(0 - je)][idk] + re[nrm(2 - je)][idk]; prmiss(1); } wy %= mod; wy += mod; wy %= mod; return wy; }; cout << gw() << endl; REP(qq, q) { int x; char cc = 0; int val = 0; cin >> x; --x; while(cc != 'C' && cc != 'Z' && cc != 'N') cin >> cc; if(cc == 'N') val = -1; else if(cc == 'Z') val = 1; else if(cc == 'C') val = 0; upd(x, ve[x], -1); upd(x, val, 1); ve[x] = val; cout << gw() << "\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 | //Franciszek Witt #include<bits/stdc++.h> using namespace std; #define FOR(i,l,r) for(int i=(l);i<=(r);++i) #define REP(i,n) FOR(i,0,(n)-1) #define ssize(x) int(x.size()) template<class A,class B>auto&operator<<(ostream&o,pair<A,B>p){return o<<'('<<p.first<<", "<<p.second<<')';} template<class T>auto operator<<(ostream&o,T x)->decltype(x.end(),o){o<<'{';int i=0;for(auto e:x)o<<(", ")+2*!i++<<e;return o<<'}';} #ifdef DEBUG #define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...);}(x),cerr<<'\n' #else #define debug(...) {} #endif typedef long long ll; const int mod = int(1e9) + 7; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, q; cin >> n >> q; auto nrm = [&] (int x) { x %= 3; if(x < 0) x += 3; return x; }; vector re(3, vector(n + 1, 0LL)); re[0][0] = 1LL; FOR(i, 1, n) REP(j, 3) re[j][i] = (re[j][i - 1] + re[nrm(j - 1)][i - 1]) % mod; int miss0 = 0, miss1 = 0, ze = 0, je = 0, idk = 0; auto upd = [&] (int x, int y, int sig) { if(y == -1) { idk += sig; return; } if((x + y) & 1) miss0 += sig; else miss1 += sig; if(y == 0) ze += sig; if(y == 1) je += sig; }; vector<int> ve; { string s; cin >> s; REP(i, n) { if(s[i] == 'C') { ve.emplace_back(0); } else if(s[i] == 'Z') { ve.emplace_back(1); } else { ve.emplace_back(-1); } upd(i, ve.back(), 1); } } auto gw = [&] () { ll wy = 0; auto prmiss = [&] (int ban) { if(n == 1) return; if((n / 2) % 3 != ban) wy -= !miss0; if(((n + 1) / 2) % 3 != ban) wy -= !miss1; }; if(n % 3 == 0) { wy = re[nrm(1 - je)][idk] + re[nrm(2 - je)][idk]; prmiss(0); } else if(n % 3 == 1) { wy = re[nrm(0 - je)][idk] + re[nrm(1 - je)][idk]; prmiss(2); } else if(n % 3 == 2) { wy = re[nrm(0 - je)][idk] + re[nrm(2 - je)][idk]; prmiss(1); } wy %= mod; wy += mod; wy %= mod; return wy; }; cout << gw() << endl; REP(qq, q) { int x; char cc = 0; int val = 0; cin >> x; --x; while(cc != 'C' && cc != 'Z' && cc != 'N') cin >> cc; if(cc == 'N') val = -1; else if(cc == 'Z') val = 1; else if(cc == 'C') val = 0; upd(x, ve[x], -1); upd(x, val, 1); ve[x] = val; cout << gw() << "\n"; } } |