#include<bits/stdc++.h> #define int long long using namespace std; constexpr int mod = 1e9 + 7; constexpr int MAXN = 2e5 + 7; int dp[MAXN][3]; int pow2[MAXN]; void count_pow2(int n) { pow2[0] = 1; for (int i = 1; i <= n; i++) pow2[i] = (pow2[i - 1] << 1) % mod; } void count_dp(int n) { dp[0][0] = 1; dp[0][1] = 0; dp[0][2] = 0; for (int i = 1; i <= n; i++){ for (int r = 0; r < 3; r++){ dp[i][r] = (pow2[i - 1] - dp[i - 1][(r + 1) % 3] + mod) % mod; } } } int get_rot(int n_rot, int num_red) { num_red %= 3; return (n_rot - num_red + 3) % 3; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, q; string s; cin >> n >> q >> s; const int n_rot = 2 - ((n - 1) % 3); const bool ignore = (n == 1) || (n % 2 == 0); count_pow2(n + 5); count_dp(n); int cantbe101 = 0; int cantbe010 = 0; int num_red = 0; int num_blue = 0; for (int i = 0; i < n; i++) { if (s[i] == 'N') { num_blue++; continue; } int curr = (s[i] == 'C'); num_red += curr; if ((curr + i) % 2 == 1) cantbe010++; else cantbe101++; } int res = (pow2[num_blue] - dp[num_blue][get_rot(n_rot, num_red)] + mod) % mod; if (!ignore && !cantbe010) res--; if (!ignore && !cantbe101) res--; res = (res + mod) % mod; cout << res << "\n"; for (int i = 1; i <= q; i++) { int where; char what; cin >> where >> what; where--; if (s[where] == 'N') num_blue--; else { int curr = (s[where] == 'C'); num_red -= curr; if ((curr + where) % 2 == 1) cantbe010--; else cantbe101--; } if (what == 'N') num_blue++; else { int curr = (what == 'C'); num_red += curr; if ((curr + where) % 2 == 1) cantbe010++; else cantbe101++; } s[where] = what; res = (pow2[num_blue] - dp[num_blue][get_rot(n_rot, num_red)] + mod) % mod; if (!ignore && !cantbe010) res--; if (!ignore && !cantbe101) res--; res = (res + mod) % mod; cout << res << "\n"; } 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 115 116 117 118 119 120 | #include<bits/stdc++.h> #define int long long using namespace std; constexpr int mod = 1e9 + 7; constexpr int MAXN = 2e5 + 7; int dp[MAXN][3]; int pow2[MAXN]; void count_pow2(int n) { pow2[0] = 1; for (int i = 1; i <= n; i++) pow2[i] = (pow2[i - 1] << 1) % mod; } void count_dp(int n) { dp[0][0] = 1; dp[0][1] = 0; dp[0][2] = 0; for (int i = 1; i <= n; i++){ for (int r = 0; r < 3; r++){ dp[i][r] = (pow2[i - 1] - dp[i - 1][(r + 1) % 3] + mod) % mod; } } } int get_rot(int n_rot, int num_red) { num_red %= 3; return (n_rot - num_red + 3) % 3; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, q; string s; cin >> n >> q >> s; const int n_rot = 2 - ((n - 1) % 3); const bool ignore = (n == 1) || (n % 2 == 0); count_pow2(n + 5); count_dp(n); int cantbe101 = 0; int cantbe010 = 0; int num_red = 0; int num_blue = 0; for (int i = 0; i < n; i++) { if (s[i] == 'N') { num_blue++; continue; } int curr = (s[i] == 'C'); num_red += curr; if ((curr + i) % 2 == 1) cantbe010++; else cantbe101++; } int res = (pow2[num_blue] - dp[num_blue][get_rot(n_rot, num_red)] + mod) % mod; if (!ignore && !cantbe010) res--; if (!ignore && !cantbe101) res--; res = (res + mod) % mod; cout << res << "\n"; for (int i = 1; i <= q; i++) { int where; char what; cin >> where >> what; where--; if (s[where] == 'N') num_blue--; else { int curr = (s[where] == 'C'); num_red -= curr; if ((curr + where) % 2 == 1) cantbe010--; else cantbe101--; } if (what == 'N') num_blue++; else { int curr = (what == 'C'); num_red += curr; if ((curr + where) % 2 == 1) cantbe010++; else cantbe101++; } s[where] = what; res = (pow2[num_blue] - dp[num_blue][get_rot(n_rot, num_red)] + mod) % mod; if (!ignore && !cantbe010) res--; if (!ignore && !cantbe101) res--; res = (res + mod) % mod; cout << res << "\n"; } return 0; } |