#include <bits/stdc++.h> using namespace std; unsigned long long PRIME = 1e9+7; unsigned long long INVERSE = 333333336; long long power(long long x, unsigned int y, int p) { long long res = 1; x = x % p; if (x == 0) return 0; // In case x is divisible by p; while (y > 0) { if (y & 1) res = (res*x) % p; y = y>>1; x = (x*x) % p; } return res; } unsigned long long binom_sum(long long n, long long p){ unsigned long long result = (unsigned long long) ( power(2, n, PRIME) + round(cos(M_PI*(n - 2*p)/3) + (n % 2 ? -1 : 1) * cos(M_PI*2*(n - 2*p)/3)) + PRIME ) % PRIME; result *= INVERSE; return result % PRIME; } unsigned long long solve(vector<int> dist, vector<int> pos_dist) { int length = dist[0] + dist[1] + dist[2]; unsigned long long counter = power(2, dist[2], PRIME); counter = (counter + PRIME - binom_sum(dist[2], ((2*length - 4*dist[1]))%3))%PRIME; if(length % 2 == 1 && pos_dist[0] == 0 && pos_dist[3] == 0) counter--; if(length % 2 == 1 && pos_dist[1] == 0 && pos_dist[2] == 0) counter--; return counter; } int main() { std::ios_base::sync_with_stdio(false); int n, q; cin >> n >> q; string chain; cin >> chain; vector<int> dist(3); vector<int> pos_dist(4); dist[0] = 0; dist[1] = 0; dist[1] = 0; pos_dist[0] = 0; pos_dist[1] = 0; pos_dist[2] = 0; pos_dist[3] = 0; for(int i = 0; i < chain.size(); i++) { if(chain[i] == 'Z'){ pos_dist[i%2]++; dist[0]++; } else if (chain[i] == 'C'){ pos_dist[2 + i%2]++; dist[1]++; } else dist[2]++; } cout << solve(dist, pos_dist) << endl; for(int i = 0; i < q; i++){ int idx; char new_char; cin >> idx >> new_char; idx--; if(chain[idx] != new_char) { if(chain[idx] == 'Z'){ pos_dist[idx%2]--; dist[0]--; } else if (chain[idx] == 'C'){ pos_dist[2 + idx%2]--; dist[1]--; } else dist[2]--; chain[idx] = new_char; if(new_char == 'Z'){ pos_dist[idx%2]++; dist[0]++; } else if (new_char == 'C'){ pos_dist[2 + idx%2]++; dist[1]++; } else dist[2]++; } cout << solve(dist, pos_dist) << endl; } 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 | #include <bits/stdc++.h> using namespace std; unsigned long long PRIME = 1e9+7; unsigned long long INVERSE = 333333336; long long power(long long x, unsigned int y, int p) { long long res = 1; x = x % p; if (x == 0) return 0; // In case x is divisible by p; while (y > 0) { if (y & 1) res = (res*x) % p; y = y>>1; x = (x*x) % p; } return res; } unsigned long long binom_sum(long long n, long long p){ unsigned long long result = (unsigned long long) ( power(2, n, PRIME) + round(cos(M_PI*(n - 2*p)/3) + (n % 2 ? -1 : 1) * cos(M_PI*2*(n - 2*p)/3)) + PRIME ) % PRIME; result *= INVERSE; return result % PRIME; } unsigned long long solve(vector<int> dist, vector<int> pos_dist) { int length = dist[0] + dist[1] + dist[2]; unsigned long long counter = power(2, dist[2], PRIME); counter = (counter + PRIME - binom_sum(dist[2], ((2*length - 4*dist[1]))%3))%PRIME; if(length % 2 == 1 && pos_dist[0] == 0 && pos_dist[3] == 0) counter--; if(length % 2 == 1 && pos_dist[1] == 0 && pos_dist[2] == 0) counter--; return counter; } int main() { std::ios_base::sync_with_stdio(false); int n, q; cin >> n >> q; string chain; cin >> chain; vector<int> dist(3); vector<int> pos_dist(4); dist[0] = 0; dist[1] = 0; dist[1] = 0; pos_dist[0] = 0; pos_dist[1] = 0; pos_dist[2] = 0; pos_dist[3] = 0; for(int i = 0; i < chain.size(); i++) { if(chain[i] == 'Z'){ pos_dist[i%2]++; dist[0]++; } else if (chain[i] == 'C'){ pos_dist[2 + i%2]++; dist[1]++; } else dist[2]++; } cout << solve(dist, pos_dist) << endl; for(int i = 0; i < q; i++){ int idx; char new_char; cin >> idx >> new_char; idx--; if(chain[idx] != new_char) { if(chain[idx] == 'Z'){ pos_dist[idx%2]--; dist[0]--; } else if (chain[idx] == 'C'){ pos_dist[2 + idx%2]--; dist[1]--; } else dist[2]--; chain[idx] = new_char; if(new_char == 'Z'){ pos_dist[idx%2]++; dist[0]++; } else if (new_char == 'C'){ pos_dist[2 + idx%2]++; dist[1]++; } else dist[2]++; } cout << solve(dist, pos_dist) << endl; } return 0; } |