#include <bits/stdc++.h> #include <random> #define ll long long int #define pb push_back #define st first #define nd second #define pii pair<int,int> #define mp make_pair #define pll pair<long long,long long> #define ld long double #define ull unsigned long long #define mt make_tuple using namespace std; const int nax = 2e5 + 5; const int mod = 1e9 + 7; int n, q; ll ways[nax][3]; // suma binomiali (n po i) dla i = j mod 3 ll pp[nax]; void add(ll &a, ll b){ a += b; a %= mod; } void prep(){ ways[0][0] = 1; for(int i=1;i<=n;i++){ for(int j=0;j<3;j++){ add(ways[i][j], ways[i - 1][j]); add(ways[i][j], ways[i - 1][(j + 2) % 3]); } } pp[0] = 1; for(int i=1;i<=n;i++){ pp[i] = (pp[i - 1] * 2) % mod; } } int parse(char s){ if(s == 'N') return 2; if(s == 'C') return 0; return 1; } int a[nax]; int marks = 0; int ban0 = 0; int ban1 = 0; int zeros = 0; int ones = 0; void upd(int tp, int pos, int znak){ if(tp == 2){ marks += znak; return; } if(tp == 1) ones += znak; else zeros += znak; if((tp + pos) % 2 == 0) ban0 += znak; else ban1 += znak; } int calc(){ int bad_mod = 0; while(bad_mod % 3 != (n - bad_mod + 9) % 3) bad_mod++; int is = ones % 3; int need_add = (bad_mod - is + 3) % 3; ll bad = ways[marks][need_add]; if(ban0 == 0){ if((n / 2) % 3 != bad_mod) add(bad, 1LL); } if(ban1 == 0){ if(((n + 1) / 2) % 3 != bad_mod) add(bad, 1LL); } ll ans = (pp[marks] - bad + mod) % mod; if(n == 1){ if(marks == 1) return 2; return 1; } return ans; } void solve(){ cin >> n >> q; prep(); string s; cin >> s; for(int i=1;i<=n;i++){ a[i] = parse(s[i - 1]); upd(a[i], i, 1); } cout << calc() << "\n"; for(int i=1;i<=q;i++){ int pos; char zn; cin >> pos >> zn; int tp = parse(zn); upd(a[pos], pos, -1); a[pos] = tp; upd(a[pos], pos, 1); cout << calc() << "\n"; } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int tt = 1; // cin >> tt; while(tt--) solve(); return 0; } /* g++ -std=c++17 -Wall -Wshadow -Wextra -O2 -fsanitize=address -fsanitize=undefined c.cpp -o c */
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 | #include <bits/stdc++.h> #include <random> #define ll long long int #define pb push_back #define st first #define nd second #define pii pair<int,int> #define mp make_pair #define pll pair<long long,long long> #define ld long double #define ull unsigned long long #define mt make_tuple using namespace std; const int nax = 2e5 + 5; const int mod = 1e9 + 7; int n, q; ll ways[nax][3]; // suma binomiali (n po i) dla i = j mod 3 ll pp[nax]; void add(ll &a, ll b){ a += b; a %= mod; } void prep(){ ways[0][0] = 1; for(int i=1;i<=n;i++){ for(int j=0;j<3;j++){ add(ways[i][j], ways[i - 1][j]); add(ways[i][j], ways[i - 1][(j + 2) % 3]); } } pp[0] = 1; for(int i=1;i<=n;i++){ pp[i] = (pp[i - 1] * 2) % mod; } } int parse(char s){ if(s == 'N') return 2; if(s == 'C') return 0; return 1; } int a[nax]; int marks = 0; int ban0 = 0; int ban1 = 0; int zeros = 0; int ones = 0; void upd(int tp, int pos, int znak){ if(tp == 2){ marks += znak; return; } if(tp == 1) ones += znak; else zeros += znak; if((tp + pos) % 2 == 0) ban0 += znak; else ban1 += znak; } int calc(){ int bad_mod = 0; while(bad_mod % 3 != (n - bad_mod + 9) % 3) bad_mod++; int is = ones % 3; int need_add = (bad_mod - is + 3) % 3; ll bad = ways[marks][need_add]; if(ban0 == 0){ if((n / 2) % 3 != bad_mod) add(bad, 1LL); } if(ban1 == 0){ if(((n + 1) / 2) % 3 != bad_mod) add(bad, 1LL); } ll ans = (pp[marks] - bad + mod) % mod; if(n == 1){ if(marks == 1) return 2; return 1; } return ans; } void solve(){ cin >> n >> q; prep(); string s; cin >> s; for(int i=1;i<=n;i++){ a[i] = parse(s[i - 1]); upd(a[i], i, 1); } cout << calc() << "\n"; for(int i=1;i<=q;i++){ int pos; char zn; cin >> pos >> zn; int tp = parse(zn); upd(a[pos], pos, -1); a[pos] = tp; upd(a[pos], pos, 1); cout << calc() << "\n"; } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int tt = 1; // cin >> tt; while(tt--) solve(); return 0; } /* g++ -std=c++17 -Wall -Wshadow -Wextra -O2 -fsanitize=address -fsanitize=undefined c.cpp -o c */ |