// Olaf Surgut 12.12.2022 18:48:09 #include <bits/stdc++.h> using namespace std; const int N = 200000 + 11; const int mod = 1e9 + 7; int n, q; char s[N]; int cnt[3][2], ile[N][3], pot[N]; int main() { scanf("%d%d", &n, &q); scanf("%s", s + 1); auto typ = [&](char c) { if (c == 'C') return 0; else if (c == 'Z') return 1; else if (c == 'N') return 2; return -1; }; for (int i = 1; i <= n; i++) { cnt[typ(s[i])][i % 2]++; } ile[0][0] = 1; pot[0] = 1; for (int i = 1; i <= n; i++) { (ile[i][0] = ile[i - 1][2] + ile[i - 1][1]) %= mod; (ile[i][1] = ile[i - 1][0] + ile[i - 1][2]) %= mod; (ile[i][2] = ile[i - 1][1] + ile[i - 1][0]) %= mod; (pot[i] = pot[i - 1] * 2) %= mod; } auto solve = [&]() { int ans = pot[cnt[2][0] + cnt[2][1]]; if (cnt[0][0] == 0 && cnt[1][1] == 0) { (ans += mod - 1) %= mod; } if (cnt[0][1] == 0 && cnt[1][0] == 0) { (ans += mod - 1) %= mod; } int r = cnt[0][0] + cnt[0][1] - cnt[1][0] - cnt[1][1]; r = ((r % 3) + 3) % 3; (ans += mod - ile[cnt[2][0] + cnt[2][1]][r]) %= mod; return ans; }; cout << solve() << '\n'; while (q--) { int p; char c; cin >> p >> c; cnt[typ(s[p])][p % 2]--; s[p] = c; cnt[typ(s[p])][p % 2]++; cout << solve() << '\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 | // Olaf Surgut 12.12.2022 18:48:09 #include <bits/stdc++.h> using namespace std; const int N = 200000 + 11; const int mod = 1e9 + 7; int n, q; char s[N]; int cnt[3][2], ile[N][3], pot[N]; int main() { scanf("%d%d", &n, &q); scanf("%s", s + 1); auto typ = [&](char c) { if (c == 'C') return 0; else if (c == 'Z') return 1; else if (c == 'N') return 2; return -1; }; for (int i = 1; i <= n; i++) { cnt[typ(s[i])][i % 2]++; } ile[0][0] = 1; pot[0] = 1; for (int i = 1; i <= n; i++) { (ile[i][0] = ile[i - 1][2] + ile[i - 1][1]) %= mod; (ile[i][1] = ile[i - 1][0] + ile[i - 1][2]) %= mod; (ile[i][2] = ile[i - 1][1] + ile[i - 1][0]) %= mod; (pot[i] = pot[i - 1] * 2) %= mod; } auto solve = [&]() { int ans = pot[cnt[2][0] + cnt[2][1]]; if (cnt[0][0] == 0 && cnt[1][1] == 0) { (ans += mod - 1) %= mod; } if (cnt[0][1] == 0 && cnt[1][0] == 0) { (ans += mod - 1) %= mod; } int r = cnt[0][0] + cnt[0][1] - cnt[1][0] - cnt[1][1]; r = ((r % 3) + 3) % 3; (ans += mod - ile[cnt[2][0] + cnt[2][1]][r]) %= mod; return ans; }; cout << solve() << '\n'; while (q--) { int p; char c; cin >> p >> c; cnt[typ(s[p])][p % 2]--; s[p] = c; cnt[typ(s[p])][p % 2]++; cout << solve() << '\n'; } } |