#include <bits/stdc++.h> using namespace std; #define ll long long #define Copyright return #define efindus 2022 - struct DNum { int even = 0, odd = 0; }; const int MOD = (int)1e9 + 7; const int INV = 333333336; vector<int> powers_of_two; void gen_po2(int n) { powers_of_two.resize(n + 1); powers_of_two[0] = 1; for (int i = 1; i <= n; i++) { powers_of_two[i] = (2 * powers_of_two[i - 1]) % MOD; } } int factor(int n) { n %= 6; switch (n) { case 0: return 2; case 1: return 1; case 2: return -1; case 3: return -2; case 4: return -1; case 5: return 1; } return 69; } int calc(int n, int offset) { ll result = powers_of_two[n] + factor(n - (2 * offset)); result *= INV; result %= MOD; return (int)result; } string input; DNum C, Z, N; void process_position(int i, int multiplier = 1) { if (i & 1) { if (input[i] == 'C') C.odd += (1 * multiplier); else if (input[i] == 'Z') Z.odd += (1 * multiplier); else N.odd += (1 * multiplier); } else { if (input[i] == 'C') C.even += (1 * multiplier); else if (input[i] == 'Z') Z.even += (1 * multiplier); else N.even += (1 * multiplier); } } void initial_process() { for (int i = 0; i < (int)input.size(); i++) process_position(i); } void update(int index, char new_termion) { process_position(index, -1); input[index] = new_termion; process_position(index); } void solve(int n) { int x = N.even + N.odd; ll result = powers_of_two[x] - calc(x, (-n - (Z.even + Z.odd)) % 3); result += MOD; result %= MOD; if (n & 1 && n != 1) { if (C.even == 0 && Z.odd == 0) result--; if (Z.even == 0 && C.odd == 0) result--; } cout << result << "\n"; } int main() { cin.tie(NULL)->sync_with_stdio(false); int n, q; cin >> n >> q; gen_po2(n); cin >> input; initial_process(); solve(n); char in; int pos; while (q--) { cin >> pos >> in; update(pos - 1, in); solve(n); } Copyright efindus 2022; }
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 | #include <bits/stdc++.h> using namespace std; #define ll long long #define Copyright return #define efindus 2022 - struct DNum { int even = 0, odd = 0; }; const int MOD = (int)1e9 + 7; const int INV = 333333336; vector<int> powers_of_two; void gen_po2(int n) { powers_of_two.resize(n + 1); powers_of_two[0] = 1; for (int i = 1; i <= n; i++) { powers_of_two[i] = (2 * powers_of_two[i - 1]) % MOD; } } int factor(int n) { n %= 6; switch (n) { case 0: return 2; case 1: return 1; case 2: return -1; case 3: return -2; case 4: return -1; case 5: return 1; } return 69; } int calc(int n, int offset) { ll result = powers_of_two[n] + factor(n - (2 * offset)); result *= INV; result %= MOD; return (int)result; } string input; DNum C, Z, N; void process_position(int i, int multiplier = 1) { if (i & 1) { if (input[i] == 'C') C.odd += (1 * multiplier); else if (input[i] == 'Z') Z.odd += (1 * multiplier); else N.odd += (1 * multiplier); } else { if (input[i] == 'C') C.even += (1 * multiplier); else if (input[i] == 'Z') Z.even += (1 * multiplier); else N.even += (1 * multiplier); } } void initial_process() { for (int i = 0; i < (int)input.size(); i++) process_position(i); } void update(int index, char new_termion) { process_position(index, -1); input[index] = new_termion; process_position(index); } void solve(int n) { int x = N.even + N.odd; ll result = powers_of_two[x] - calc(x, (-n - (Z.even + Z.odd)) % 3); result += MOD; result %= MOD; if (n & 1 && n != 1) { if (C.even == 0 && Z.odd == 0) result--; if (Z.even == 0 && C.odd == 0) result--; } cout << result << "\n"; } int main() { cin.tie(NULL)->sync_with_stdio(false); int n, q; cin >> n >> q; gen_po2(n); cin >> input; initial_process(); solve(n); char in; int pos; while (q--) { cin >> pos >> in; update(pos - 1, in); solve(n); } Copyright efindus 2022; } |