#include <iostream>
using namespace std;
using ll = long long;
constexpr ll mod = 1000000007;
ll thirds[3][200200];
ll pot[200200];
void init() {
pot[0] = 1;
for(int i = 1; i < 200100; i++) {
pot[i] = (pot[i - 1] * 2) % mod;
}
thirds[0][0] = 1;
for(int i = 1; i < 200100; i++) {
for(int j = 0; j < 3; j++) {
thirds[j][i] = (pot[i - 1] - thirds[(j + 1) % 3][i - 1] + mod) % mod;
}
}
}
int whatLetter(char c) {
switch (c) {
case 'C':
return 0;
case 'Z':
return 1;
case 'N':
return 2;
default:
return 2;
}
}
string s;
int n, q;
int onParity[2][3];
int il[3];
ll result() {
if (n == 1) return 1;
ll wyn = pot[il[2]];
//cout << " " << wyn;
if (n % 2 == 1) {
if (!onParity[0][0] && !onParity[1][1]) wyn--;
if (!onParity[1][0] && !onParity[0][1]) wyn--;
}
//cout << " " << wyn;
wyn = wyn - thirds[(((il[2] + il[1] - il[0]) * 2) % 3 + 3) % 3][il[2]];
//cout << " " << wyn;
wyn = (wyn % mod + mod) % mod;
//cout << " " << wyn;
//cout << "\n";
return wyn;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
init();
cin >> n >> q;
cin >> s;
for (int i = 0; i < n; i++) {
int what = whatLetter(s[i]);
onParity[i % 2][what]++;
il[what]++;
}
cout << result() << "\n";
int a;
string zm;
for (int i = 0; i < q; i++) {
cin >> a >> zm;
a--;
int old = whatLetter(s[a]), news = whatLetter(zm[0]);
s[a] = zm[0];
onParity[a % 2][old]--;
onParity[a % 2][news]++;
il[old]--;
il[news]++;
cout << result() << "\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 | #include <iostream> using namespace std; using ll = long long; constexpr ll mod = 1000000007; ll thirds[3][200200]; ll pot[200200]; void init() { pot[0] = 1; for(int i = 1; i < 200100; i++) { pot[i] = (pot[i - 1] * 2) % mod; } thirds[0][0] = 1; for(int i = 1; i < 200100; i++) { for(int j = 0; j < 3; j++) { thirds[j][i] = (pot[i - 1] - thirds[(j + 1) % 3][i - 1] + mod) % mod; } } } int whatLetter(char c) { switch (c) { case 'C': return 0; case 'Z': return 1; case 'N': return 2; default: return 2; } } string s; int n, q; int onParity[2][3]; int il[3]; ll result() { if (n == 1) return 1; ll wyn = pot[il[2]]; //cout << " " << wyn; if (n % 2 == 1) { if (!onParity[0][0] && !onParity[1][1]) wyn--; if (!onParity[1][0] && !onParity[0][1]) wyn--; } //cout << " " << wyn; wyn = wyn - thirds[(((il[2] + il[1] - il[0]) * 2) % 3 + 3) % 3][il[2]]; //cout << " " << wyn; wyn = (wyn % mod + mod) % mod; //cout << " " << wyn; //cout << "\n"; return wyn; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); init(); cin >> n >> q; cin >> s; for (int i = 0; i < n; i++) { int what = whatLetter(s[i]); onParity[i % 2][what]++; il[what]++; } cout << result() << "\n"; int a; string zm; for (int i = 0; i < q; i++) { cin >> a >> zm; a--; int old = whatLetter(s[a]), news = whatLetter(zm[0]); s[a] = zm[0]; onParity[a % 2][old]--; onParity[a % 2][news]++; il[old]--; il[news]++; cout << result() << "\n"; } } |
English