#include <bits/stdc++.h> using namespace std; #define int int64_t #define pb push_back #define st first #define nd second #define pii pair<int, int> #define all(x) (x).begin(), (x).end() #define sz(x) (int)(x).size() const int MOD = 1e9+7; int pot[200009]; int bin[200009][3]; int tab[200009]; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); pot[0] = 1; pot[1] = 2; pot[2] = 4; bin[0][0] = bin[1][0] = bin[2][0] = 1; bin[0][1] = 0; bin[1][1] = 1; bin[2][1] = 2; bin[0][2] = bin[1][2] = 0; bin[2][2] = 1; for(int i=3;i<200009;i++) { pot[i] = pot[i-1]*2%MOD; bin[i][0] = (3*bin[i-1][0]-3*bin[i-2][0]+2*bin[i-3][0])%MOD; if(bin[i][0] < 0) bin[i][0]+=MOD; bin[i][1] = (3*bin[i-1][1]-3*bin[i-2][1]+2*bin[i-3][1])%MOD; if(bin[i][1] < 0) bin[i][1]+=MOD; bin[i][2] = (pot[i]-bin[i][0]-bin[i][1])%MOD; if(bin[i][2] < 0) bin[i][2]+=MOD; } int n, q; cin >> n >> q; if(n==1) { char c; cin >> c; if(c == 'N') { cout << "2\n"; } else { cout << "1\n"; } while(q--) { int x; cin >> x >> c; if(c == 'N') { cout << "2\n"; } else { cout << "1\n"; } } return 0; } int a=0, b=0, cnt=0; int a_par[] = {0, 0}; int b_par[] = {0, 0}; for(int i=0;i<n;i++) { char c; cin >> c; if(c == 'N') { tab[i] = -1; cnt++; } else if(c == 'C') { tab[i] = 0; a++; a_par[i%2]++; } else { tab[i] = 1; b++; b_par[i%2]++; } } int tmp = ((a-b)%3-cnt%3)%3; if(tmp < 0) tmp += 3; int ans = pot[cnt] - bin[cnt][tmp]; if(n%2) { int A, B; if(a_par[0]==0 and a_par[1]==0) A = 0; if(a_par[0]!=0 and a_par[1]==0) A = 1; if(a_par[0]==0 and a_par[1]!=0) A = 2; if(a_par[0]!=0 and a_par[1]!=0) A = 3; if(b_par[0]==0 and b_par[1]==0) B = 0; if(b_par[0]!=0 and b_par[1]==0) B = 1; if(b_par[0]==0 and b_par[1]!=0) B = 2; if(b_par[0]!=0 and b_par[1]!=0) B = 3; if(A==0&&B==0) ans -= 2; if(A!=3&&B!=3&&A!=B) ans -=1; } ans %= MOD; if(ans < 0) ans += MOD; cout << ans << "\n"; for(int i=0;i<q;i++) { int x; char c; cin >> x >> c; x--; if(tab[x] == -1) { cnt--; } else if(tab[x] == 0) { a--; a_par[x%2]--; } else { b--; b_par[x%2]--; } if(c == 'N') { tab[x] = -1; cnt++; } else if(c == 'C') { tab[x] = 0; a++; a_par[x%2]++; } else { tab[x] = 1; b++; b_par[x%2]++; } tmp = ((a-b)%3-cnt%3)%3; if(tmp < 0) tmp += 3; ans = pot[cnt] - bin[cnt][tmp]; if(n%2) { int A, B; if(a_par[0]==0 and a_par[1]==0) A = 0; if(a_par[0]!=0 and a_par[1]==0) A = 1; if(a_par[0]==0 and a_par[1]!=0) A = 2; if(a_par[0]!=0 and a_par[1]!=0) A = 3; if(b_par[0]==0 and b_par[1]==0) B = 0; if(b_par[0]!=0 and b_par[1]==0) B = 1; if(b_par[0]==0 and b_par[1]!=0) B = 2; if(b_par[0]!=0 and b_par[1]!=0) B = 3; if(A==0&&B==0) ans -= 2; if(A!=3&&B!=3&&A!=B) ans -=1; } ans %= MOD; if(ans < 0) ans += MOD; cout << ans << "\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 | #include <bits/stdc++.h> using namespace std; #define int int64_t #define pb push_back #define st first #define nd second #define pii pair<int, int> #define all(x) (x).begin(), (x).end() #define sz(x) (int)(x).size() const int MOD = 1e9+7; int pot[200009]; int bin[200009][3]; int tab[200009]; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); pot[0] = 1; pot[1] = 2; pot[2] = 4; bin[0][0] = bin[1][0] = bin[2][0] = 1; bin[0][1] = 0; bin[1][1] = 1; bin[2][1] = 2; bin[0][2] = bin[1][2] = 0; bin[2][2] = 1; for(int i=3;i<200009;i++) { pot[i] = pot[i-1]*2%MOD; bin[i][0] = (3*bin[i-1][0]-3*bin[i-2][0]+2*bin[i-3][0])%MOD; if(bin[i][0] < 0) bin[i][0]+=MOD; bin[i][1] = (3*bin[i-1][1]-3*bin[i-2][1]+2*bin[i-3][1])%MOD; if(bin[i][1] < 0) bin[i][1]+=MOD; bin[i][2] = (pot[i]-bin[i][0]-bin[i][1])%MOD; if(bin[i][2] < 0) bin[i][2]+=MOD; } int n, q; cin >> n >> q; if(n==1) { char c; cin >> c; if(c == 'N') { cout << "2\n"; } else { cout << "1\n"; } while(q--) { int x; cin >> x >> c; if(c == 'N') { cout << "2\n"; } else { cout << "1\n"; } } return 0; } int a=0, b=0, cnt=0; int a_par[] = {0, 0}; int b_par[] = {0, 0}; for(int i=0;i<n;i++) { char c; cin >> c; if(c == 'N') { tab[i] = -1; cnt++; } else if(c == 'C') { tab[i] = 0; a++; a_par[i%2]++; } else { tab[i] = 1; b++; b_par[i%2]++; } } int tmp = ((a-b)%3-cnt%3)%3; if(tmp < 0) tmp += 3; int ans = pot[cnt] - bin[cnt][tmp]; if(n%2) { int A, B; if(a_par[0]==0 and a_par[1]==0) A = 0; if(a_par[0]!=0 and a_par[1]==0) A = 1; if(a_par[0]==0 and a_par[1]!=0) A = 2; if(a_par[0]!=0 and a_par[1]!=0) A = 3; if(b_par[0]==0 and b_par[1]==0) B = 0; if(b_par[0]!=0 and b_par[1]==0) B = 1; if(b_par[0]==0 and b_par[1]!=0) B = 2; if(b_par[0]!=0 and b_par[1]!=0) B = 3; if(A==0&&B==0) ans -= 2; if(A!=3&&B!=3&&A!=B) ans -=1; } ans %= MOD; if(ans < 0) ans += MOD; cout << ans << "\n"; for(int i=0;i<q;i++) { int x; char c; cin >> x >> c; x--; if(tab[x] == -1) { cnt--; } else if(tab[x] == 0) { a--; a_par[x%2]--; } else { b--; b_par[x%2]--; } if(c == 'N') { tab[x] = -1; cnt++; } else if(c == 'C') { tab[x] = 0; a++; a_par[x%2]++; } else { tab[x] = 1; b++; b_par[x%2]++; } tmp = ((a-b)%3-cnt%3)%3; if(tmp < 0) tmp += 3; ans = pot[cnt] - bin[cnt][tmp]; if(n%2) { int A, B; if(a_par[0]==0 and a_par[1]==0) A = 0; if(a_par[0]!=0 and a_par[1]==0) A = 1; if(a_par[0]==0 and a_par[1]!=0) A = 2; if(a_par[0]!=0 and a_par[1]!=0) A = 3; if(b_par[0]==0 and b_par[1]==0) B = 0; if(b_par[0]!=0 and b_par[1]==0) B = 1; if(b_par[0]==0 and b_par[1]!=0) B = 2; if(b_par[0]!=0 and b_par[1]!=0) B = 3; if(A==0&&B==0) ans -= 2; if(A!=3&&B!=3&&A!=B) ans -=1; } ans %= MOD; if(ans < 0) ans += MOD; cout << ans << "\n"; } } |