#include <bits/stdc++.h> #define dbg(x) " [" << #x << ": " << (x) << "] " using namespace std; template<typename A,typename B> ostream& operator<<(ostream& out,const pair<A,B>& p) { return out << "(" << p.first << ", " << p.second << ")"; } template<typename T> ostream& operator<<(ostream& out,const vector<T>& c) { out << "{"; for(auto it = c.begin(); it != c.end(); it++) { if(it != c.begin()) out << ", "; out << *it; } return out << "}"; } string s; int n, b; int r[2]; int g[2]; vector<vector<int>> f; void add(int i) { if(s[i] == 'C') { r[i & 1]++; } else if(s[i] == 'Z') { g[i & 1]++; } else { b++; } } void del(int i) { if(s[i] == 'C') { r[i & 1]--; } else if(s[i] == 'Z') { g[i & 1]--; } else { b--; } } const int mod = 1e9 + 7; void print() { int ans; if(n == 1) { ans = (b == 1 ? 2 : 1); } else { int x = (r[0] + r[1] + 2 * (g[0] + g[1]) + b) % 3; x = (3 - x) % 3; ans = (f[b][(x + 1) % 3] + f[b][(x + 2) % 3]) % mod; if(r[0] && r[1]) { } else if(g[0] && g[1]) { } else if(r[0] && g[0]) { } else if(r[1] && g[1]) { } else if(b == n) { if((n + n / 2) % 3 != 0) { ans = (ans + mod - 1) % mod; } if((n + (n + 1) / 2) % 3 != 0) { ans = (ans + mod - 1) % mod; } } else { int gr = (n + 1) / 2; if(r[0] || g[1]) { gr = n / 2; } if((n + gr) % 3 != 0) { ans = (ans + mod - 1) % mod; } } } cout << ans << '\n'; } void solve() { int q; cin >> n >> q; cin >> s; f.resize(n + 1); f[0] = {1, 0, 0}; for(int i = 1; i < n + 1; i++) { f[i].resize(3); for(int j = 0; j < 3; j++) { f[i][j] = (f[i - 1][j] + f[i - 1][(j + 2) % 3]) % mod; } } r[0] = r[1] = g[0] = g[1] = b = 0; for(int i = 0; i < n; i++) { add(i); } print(); while(q--) { int i; char c; cin >> i >> c; i--; del(i); s[i] = c; add(i); print(); } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int testNum = 1; //cin >> testNum; for(int i = 1; i <= testNum; i++) { //cout << "Case #" << i << ": "; solve(); } 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 | #include <bits/stdc++.h> #define dbg(x) " [" << #x << ": " << (x) << "] " using namespace std; template<typename A,typename B> ostream& operator<<(ostream& out,const pair<A,B>& p) { return out << "(" << p.first << ", " << p.second << ")"; } template<typename T> ostream& operator<<(ostream& out,const vector<T>& c) { out << "{"; for(auto it = c.begin(); it != c.end(); it++) { if(it != c.begin()) out << ", "; out << *it; } return out << "}"; } string s; int n, b; int r[2]; int g[2]; vector<vector<int>> f; void add(int i) { if(s[i] == 'C') { r[i & 1]++; } else if(s[i] == 'Z') { g[i & 1]++; } else { b++; } } void del(int i) { if(s[i] == 'C') { r[i & 1]--; } else if(s[i] == 'Z') { g[i & 1]--; } else { b--; } } const int mod = 1e9 + 7; void print() { int ans; if(n == 1) { ans = (b == 1 ? 2 : 1); } else { int x = (r[0] + r[1] + 2 * (g[0] + g[1]) + b) % 3; x = (3 - x) % 3; ans = (f[b][(x + 1) % 3] + f[b][(x + 2) % 3]) % mod; if(r[0] && r[1]) { } else if(g[0] && g[1]) { } else if(r[0] && g[0]) { } else if(r[1] && g[1]) { } else if(b == n) { if((n + n / 2) % 3 != 0) { ans = (ans + mod - 1) % mod; } if((n + (n + 1) / 2) % 3 != 0) { ans = (ans + mod - 1) % mod; } } else { int gr = (n + 1) / 2; if(r[0] || g[1]) { gr = n / 2; } if((n + gr) % 3 != 0) { ans = (ans + mod - 1) % mod; } } } cout << ans << '\n'; } void solve() { int q; cin >> n >> q; cin >> s; f.resize(n + 1); f[0] = {1, 0, 0}; for(int i = 1; i < n + 1; i++) { f[i].resize(3); for(int j = 0; j < 3; j++) { f[i][j] = (f[i - 1][j] + f[i - 1][(j + 2) % 3]) % mod; } } r[0] = r[1] = g[0] = g[1] = b = 0; for(int i = 0; i < n; i++) { add(i); } print(); while(q--) { int i; char c; cin >> i >> c; i--; del(i); s[i] = c; add(i); print(); } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int testNum = 1; //cin >> testNum; for(int i = 1; i <= testNum; i++) { //cout << "Case #" << i << ": "; solve(); } return 0; } |