#pragma GCC optimize ("Ofast") #define _USE_MATH_DEFINES #include <bits/stdc++.h> #define FOR(i, a, b) for (auto i=(a); i<(b); i++) #define FORD(i, a, b) for (BigNum i=(a); i>(b); i--) #define SZ(x) ((int)(x).size()) #define ALL(x) (x).begin(), (x).end() #define PPC(x) __builtin_popcountll(x) #define MSB(x) (63 - __builtin_clzll(x)) #define LSB(x) __builtin_ctzll(x) #define ARG(x, i) (get<i>(x)) #define ithBit(m, i) ((m) >> (i) & 1) #define pb push_back #define ft first #define sd second #define kw(a) ((a) * (a)) #ifdef DEBUG #include "debug.h" #else #define dbg(...) 0 #endif using namespace std; template <typename T1, typename T2> inline void remin(T1& a, T2 b) { a = min(a, (T1)b); } template <typename T1, typename T2> inline void remax(T1& a, T2 b) { a = max(a, (T1)b); } const int maxN = 1 << 18, mod = 1'000'000'007; char T[maxN]; int n; struct StuckChecker { char banned[maxN]; int collisions = 0; void init(char c) { char xr = 'C' ^ 'Z'; FOR(i, 0, n) { banned[i] = c; if (c == T[i]) collisions++; c ^= xr; } } void updt(int i, char c) { if (T[i] != banned[i] and c == banned[i]) collisions++; if (T[i] == banned[i] and c != banned[i]) collisions--; } bool stuckPossible() { return n % 2 == 1 and collisions == 0; } } S[2]; long long dp[maxN][3]; void calcDp() { dp[0][0] = 1; dp[1][0] = dp[1][1] = 1; FOR(i, 2, n+1) FOR(j, 0, 3) dp[i][j] = (dp[i-1][j] + dp[i-1][(j+2) % 3]) % mod; long long pow2 = 1ll; FOR(i, 0, n+1) { assert((dp[i][0] + dp[i][1] + dp[i][2]) % mod == pow2); pow2 = pow2 * 2 % mod; } } long long query(int& r, int& g, int& b) { long long ret = 0; FOR(s, 0, min(b+1,3)) if ((r + s) % 3 != (g + b-s) % 3) ret += dp[b][s]; FOR(s, 0, 2) if (S[s].stuckPossible()) ret += mod-1; return ret % mod; } void solve() { int q; scanf ("%d%d%s", &n, &q, T); S[0].init('C'), S[1].init('Z'); calcDp(); map <char, int> M; for (char c : "CZN") M[c] = std::count(T, T+n, c); int& r = M['C']; int& g = M['Z']; int& b = M['N']; printf("%lld\n", query(r, g, b)); while (q--) { int i; char buff[42]; scanf("%d%s", &i, buff); char c = buff[0]; i--; FOR(s, 0, 2) S[s].updt(i, c); M[T[i]]--; T[i] = c; M[T[i]]++; printf("%lld\n", query(r, g, b)); } } int main() { int t = 1; //scanf ("%d", &t); FOR(tid, 1, t+1) { //printf("Case #%d: ", tid); 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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | #pragma GCC optimize ("Ofast") #define _USE_MATH_DEFINES #include <bits/stdc++.h> #define FOR(i, a, b) for (auto i=(a); i<(b); i++) #define FORD(i, a, b) for (BigNum i=(a); i>(b); i--) #define SZ(x) ((int)(x).size()) #define ALL(x) (x).begin(), (x).end() #define PPC(x) __builtin_popcountll(x) #define MSB(x) (63 - __builtin_clzll(x)) #define LSB(x) __builtin_ctzll(x) #define ARG(x, i) (get<i>(x)) #define ithBit(m, i) ((m) >> (i) & 1) #define pb push_back #define ft first #define sd second #define kw(a) ((a) * (a)) #ifdef DEBUG #include "debug.h" #else #define dbg(...) 0 #endif using namespace std; template <typename T1, typename T2> inline void remin(T1& a, T2 b) { a = min(a, (T1)b); } template <typename T1, typename T2> inline void remax(T1& a, T2 b) { a = max(a, (T1)b); } const int maxN = 1 << 18, mod = 1'000'000'007; char T[maxN]; int n; struct StuckChecker { char banned[maxN]; int collisions = 0; void init(char c) { char xr = 'C' ^ 'Z'; FOR(i, 0, n) { banned[i] = c; if (c == T[i]) collisions++; c ^= xr; } } void updt(int i, char c) { if (T[i] != banned[i] and c == banned[i]) collisions++; if (T[i] == banned[i] and c != banned[i]) collisions--; } bool stuckPossible() { return n % 2 == 1 and collisions == 0; } } S[2]; long long dp[maxN][3]; void calcDp() { dp[0][0] = 1; dp[1][0] = dp[1][1] = 1; FOR(i, 2, n+1) FOR(j, 0, 3) dp[i][j] = (dp[i-1][j] + dp[i-1][(j+2) % 3]) % mod; long long pow2 = 1ll; FOR(i, 0, n+1) { assert((dp[i][0] + dp[i][1] + dp[i][2]) % mod == pow2); pow2 = pow2 * 2 % mod; } } long long query(int& r, int& g, int& b) { long long ret = 0; FOR(s, 0, min(b+1,3)) if ((r + s) % 3 != (g + b-s) % 3) ret += dp[b][s]; FOR(s, 0, 2) if (S[s].stuckPossible()) ret += mod-1; return ret % mod; } void solve() { int q; scanf ("%d%d%s", &n, &q, T); S[0].init('C'), S[1].init('Z'); calcDp(); map <char, int> M; for (char c : "CZN") M[c] = std::count(T, T+n, c); int& r = M['C']; int& g = M['Z']; int& b = M['N']; printf("%lld\n", query(r, g, b)); while (q--) { int i; char buff[42]; scanf("%d%s", &i, buff); char c = buff[0]; i--; FOR(s, 0, 2) S[s].updt(i, c); M[T[i]]--; T[i] = c; M[T[i]]++; printf("%lld\n", query(r, g, b)); } } int main() { int t = 1; //scanf ("%d", &t); FOR(tid, 1, t+1) { //printf("Case #%d: ", tid); solve(); } return 0; } |