// clang-format off #include <bits/stdc++.h> using namespace std; #define ALL(x) (x).begin(), (x).end() template<class C> void mini(C &a5, C b5) { a5 = min(a5, b5); } template<class C> void maxi(C &a5, C b5) { a5 = max(a5, b5); } #ifdef LOCAL const bool debug = true; #else const bool debug = false; #define cerr if (true) {} else cout #endif // clang-format on #define int long long #define double long double const int INF = 1e18; const int mod = 1e9 + 7; const int nax = 1e6 + 100; char arr[nax]; int counts[nax]; int counts2[2][nax]; int fib[nax][3]; int cur_pot = 1; int N, Q; int pot(int n, int k=mod-2) { int res = 1; while (k) { if (k % 2) { res = (res * n) % mod; } k /= 2; n = (n * n) % mod; } return res; } int solve() { int res = 0; int c = counts[(int)'C']; int z = counts[(int)'Z']; const int b = counts[(int)'N']; //int bb = b; if (c < z) { swap(c, z); } cerr << " " << c << " " << b << " " << z << " "; // różnica podzielna przez 3 int diff = c + b - z; res += fib[b][(3-diff%3)%3]; cerr << " /3:" << res << " " << b << " " << (3-diff)%3 << " "; /*while (diff % 3) { diff++; bb--; } if (bb >= 0) { res += bb / 3; // tu jakieś dwumiany jeszcze trzeba }*/ // naprzemienne kolory if (N % 2 && N > 1) { if (counts2[0][(int)'C'] == 0 && counts2[1][(int)'Z'] == 0) { res++; cerr << "k1:" << res << " "; } if (counts2[1][(int)'C'] == 0 && counts2[0][(int)'Z'] == 0) { res++; cerr << "k2:" << res << " "; } } cerr << "\n"; return (cur_pot + mod + mod - res) % mod; } void init() { fib[0][0] = 1; for (int i = 1; i < N + 10; i++) { for (int j = 0; j < 3; j++) { fib[i][j] = (fib[i-1][j] + fib[i-1][(j + 2) % 3]) % mod; } } for (int i = 0; i < 100; i++) { cerr << i << ": "; for (int j = 0; j < 3; j++) { cerr << fib[i][j] << " "; } cerr << "\n"; } } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); //cerr << (int)'Z' << " " << (int)'C' << " " << (int)'N' << "\n"; //return 0; int odw2 = pot(2); cin >> N >> Q; for (int i = 1; i <= N; i++) { cin >> arr[i]; counts[(int)arr[i]]++; counts2[i % 2][(int)arr[i]]++; if (arr[i] == 'N') { cur_pot = (cur_pot * 2) % mod; } } init(); cout << solve() << "\n"; while (Q--) { int pos; char c; cin >> pos >> c; counts[(int)arr[pos]]--; counts2[pos % 2][(int)arr[pos]]--; if (arr[pos] == 'N') { cur_pot = (cur_pot * odw2) % mod; } arr[pos] = c; counts[(int)arr[pos]]++; counts2[pos % 2][(int)arr[pos]]++; if (c == 'N') { cur_pot = (cur_pot * 2) % mod; } cout << solve() << "\n"; } return 0; }/* 5 3 NNCCZ 3 N 2 Z 1 Z 3 5 3 1 1 3 N 1 Z 1 C 1 N * 4 1 NNNN 1 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 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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | // clang-format off #include <bits/stdc++.h> using namespace std; #define ALL(x) (x).begin(), (x).end() template<class C> void mini(C &a5, C b5) { a5 = min(a5, b5); } template<class C> void maxi(C &a5, C b5) { a5 = max(a5, b5); } #ifdef LOCAL const bool debug = true; #else const bool debug = false; #define cerr if (true) {} else cout #endif // clang-format on #define int long long #define double long double const int INF = 1e18; const int mod = 1e9 + 7; const int nax = 1e6 + 100; char arr[nax]; int counts[nax]; int counts2[2][nax]; int fib[nax][3]; int cur_pot = 1; int N, Q; int pot(int n, int k=mod-2) { int res = 1; while (k) { if (k % 2) { res = (res * n) % mod; } k /= 2; n = (n * n) % mod; } return res; } int solve() { int res = 0; int c = counts[(int)'C']; int z = counts[(int)'Z']; const int b = counts[(int)'N']; //int bb = b; if (c < z) { swap(c, z); } cerr << " " << c << " " << b << " " << z << " "; // różnica podzielna przez 3 int diff = c + b - z; res += fib[b][(3-diff%3)%3]; cerr << " /3:" << res << " " << b << " " << (3-diff)%3 << " "; /*while (diff % 3) { diff++; bb--; } if (bb >= 0) { res += bb / 3; // tu jakieś dwumiany jeszcze trzeba }*/ // naprzemienne kolory if (N % 2 && N > 1) { if (counts2[0][(int)'C'] == 0 && counts2[1][(int)'Z'] == 0) { res++; cerr << "k1:" << res << " "; } if (counts2[1][(int)'C'] == 0 && counts2[0][(int)'Z'] == 0) { res++; cerr << "k2:" << res << " "; } } cerr << "\n"; return (cur_pot + mod + mod - res) % mod; } void init() { fib[0][0] = 1; for (int i = 1; i < N + 10; i++) { for (int j = 0; j < 3; j++) { fib[i][j] = (fib[i-1][j] + fib[i-1][(j + 2) % 3]) % mod; } } for (int i = 0; i < 100; i++) { cerr << i << ": "; for (int j = 0; j < 3; j++) { cerr << fib[i][j] << " "; } cerr << "\n"; } } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); //cerr << (int)'Z' << " " << (int)'C' << " " << (int)'N' << "\n"; //return 0; int odw2 = pot(2); cin >> N >> Q; for (int i = 1; i <= N; i++) { cin >> arr[i]; counts[(int)arr[i]]++; counts2[i % 2][(int)arr[i]]++; if (arr[i] == 'N') { cur_pot = (cur_pot * 2) % mod; } } init(); cout << solve() << "\n"; while (Q--) { int pos; char c; cin >> pos >> c; counts[(int)arr[pos]]--; counts2[pos % 2][(int)arr[pos]]--; if (arr[pos] == 'N') { cur_pot = (cur_pot * odw2) % mod; } arr[pos] = c; counts[(int)arr[pos]]++; counts2[pos % 2][(int)arr[pos]]++; if (c == 'N') { cur_pot = (cur_pot * 2) % mod; } cout << solve() << "\n"; } return 0; }/* 5 3 NNCCZ 3 N 2 Z 1 Z 3 5 3 1 1 3 N 1 Z 1 C 1 N * 4 1 NNNN 1 N */ |