#include <bits/stdc++.h> using namespace std; long long MOD = 1000000007; int c=0,z=0,cp=0,cn=0,zp=0,zn=0; int n, q; short type[3]; long long fact[200001]; long long sum[200001][3]; string str; long long gcdExtended(long long a, long long b, long long *x, long long *y) { if (a == 0) { *x = 0, *y = 1; return b; } long long x1, y1; long long gcd = gcdExtended(b%a, a, &x1, &y1); *x = y1 - (b/a) * x1; *y = x1; return gcd; } long long modInverse(long long b, long long m) { long long x, y; long long g = gcdExtended(b, m, &x, &y); if (g != 1) return -1; return (x%m + m) % m; } long long modDivide(long long a, long long b) { a = a % MOD; long long inv = modInverse(b, MOD); if (inv != -1) { return (inv * a) % MOD; } cout << "ERROR"; return 0; } long long choose(long long n, long long k) { return modDivide(modDivide(fact[n], fact[k]), fact[n-k]); } //0-C, 1-Z, 2-CZ/ZC int checkType(int c, int z) { while(c>1 || z>1) { if(c>1) { z += c/2; c %= 2; } if(z>1) { c += z/2; z %= 2; } } if(c==1) { if(z==1) { return 2; } else { return 0; } } else { return 1; } } bool correct(int c) { return type[c%3] <= 1; } long long dec(long long ret) { if(n>1 && correct(n/2)) { if(cp==0 && zn==0) ret--; if(cn==0 && zp==0) ret--; if(ret < 0) ret += MOD; } return ret; } int main() { cin >> n >> q >> str; for(int i=0;i<n;i++) { char ch = str[i]; if(ch == 'C') { c++; if(i%2==0) { cp++; } else { cn++; } } else if(ch == 'Z') { z++; if(i%2==0) { zp++; } else { zn++; } } } for(int i=0;i<=min(n,2);i++) { type[i] = checkType(i, n-i); } fact[0] = 1; for(int i=1;i<=n;i++) { fact[i] = (fact[i-1] * i) % MOD; } sum[0][0]=1; sum[0][1]=0; sum[0][2]=0; for(int i=1;i<=n;i++) { for(int j=0;j<=2;j++) { for(int k=0;k<=2;k++) { if(k == j || k == j-1 || k == j+2) { sum[i][j] += sum[i-1][k]; } } /*if(j==0) { sum[i][j]++; } if(j==(i-1)%3) { sum[i][j]++; }*/ sum[i][j]%=MOD; } //cout << i << ". " << sum[i][0] <<"+"<< sum[i][1] <<"+"<< sum[i][2]<<"="<<(sum[i][0] + sum[i][1] + sum[i][2]) << "\n"; } long long ret = 0; int N = n-c-z; for(int i=0;i<=2;i++) { if(correct(c+i)) { ret = (ret + sum[N][i]) % MOD; } } /*for(int i=0;i<=N;i++) { if(correct(c + i)) { ret = (ret + choose(N, i)) % MOD; cout << i << ": " << choose(N, i) << "\n"; } }*/ cout << dec(ret) << "\n"; int pos; char ch; for(int qq=0;qq<q;qq++) { int deltaC=0, deltaZ=0; cin >> pos >> ch; pos--; if(str[pos] == 'C') { deltaC--; } else if(str[pos] == 'Z') { deltaZ--; } str[pos] = ch; if(str[pos] == 'C') { deltaC++; } else if(str[pos] == 'Z') { deltaZ++; } c += deltaC; z += deltaZ; if(pos%2==0) { cp += deltaC; zp += deltaZ; } else { cn += deltaC; zn += deltaZ; } N = n-c-z; ret = 0; for(int i=0;i<=2;i++) { if(correct(c+i)) { ret = (ret + sum[N][i]) % MOD; } } cout << dec(ret) << "\n"; } 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 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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | #include <bits/stdc++.h> using namespace std; long long MOD = 1000000007; int c=0,z=0,cp=0,cn=0,zp=0,zn=0; int n, q; short type[3]; long long fact[200001]; long long sum[200001][3]; string str; long long gcdExtended(long long a, long long b, long long *x, long long *y) { if (a == 0) { *x = 0, *y = 1; return b; } long long x1, y1; long long gcd = gcdExtended(b%a, a, &x1, &y1); *x = y1 - (b/a) * x1; *y = x1; return gcd; } long long modInverse(long long b, long long m) { long long x, y; long long g = gcdExtended(b, m, &x, &y); if (g != 1) return -1; return (x%m + m) % m; } long long modDivide(long long a, long long b) { a = a % MOD; long long inv = modInverse(b, MOD); if (inv != -1) { return (inv * a) % MOD; } cout << "ERROR"; return 0; } long long choose(long long n, long long k) { return modDivide(modDivide(fact[n], fact[k]), fact[n-k]); } //0-C, 1-Z, 2-CZ/ZC int checkType(int c, int z) { while(c>1 || z>1) { if(c>1) { z += c/2; c %= 2; } if(z>1) { c += z/2; z %= 2; } } if(c==1) { if(z==1) { return 2; } else { return 0; } } else { return 1; } } bool correct(int c) { return type[c%3] <= 1; } long long dec(long long ret) { if(n>1 && correct(n/2)) { if(cp==0 && zn==0) ret--; if(cn==0 && zp==0) ret--; if(ret < 0) ret += MOD; } return ret; } int main() { cin >> n >> q >> str; for(int i=0;i<n;i++) { char ch = str[i]; if(ch == 'C') { c++; if(i%2==0) { cp++; } else { cn++; } } else if(ch == 'Z') { z++; if(i%2==0) { zp++; } else { zn++; } } } for(int i=0;i<=min(n,2);i++) { type[i] = checkType(i, n-i); } fact[0] = 1; for(int i=1;i<=n;i++) { fact[i] = (fact[i-1] * i) % MOD; } sum[0][0]=1; sum[0][1]=0; sum[0][2]=0; for(int i=1;i<=n;i++) { for(int j=0;j<=2;j++) { for(int k=0;k<=2;k++) { if(k == j || k == j-1 || k == j+2) { sum[i][j] += sum[i-1][k]; } } /*if(j==0) { sum[i][j]++; } if(j==(i-1)%3) { sum[i][j]++; }*/ sum[i][j]%=MOD; } //cout << i << ". " << sum[i][0] <<"+"<< sum[i][1] <<"+"<< sum[i][2]<<"="<<(sum[i][0] + sum[i][1] + sum[i][2]) << "\n"; } long long ret = 0; int N = n-c-z; for(int i=0;i<=2;i++) { if(correct(c+i)) { ret = (ret + sum[N][i]) % MOD; } } /*for(int i=0;i<=N;i++) { if(correct(c + i)) { ret = (ret + choose(N, i)) % MOD; cout << i << ": " << choose(N, i) << "\n"; } }*/ cout << dec(ret) << "\n"; int pos; char ch; for(int qq=0;qq<q;qq++) { int deltaC=0, deltaZ=0; cin >> pos >> ch; pos--; if(str[pos] == 'C') { deltaC--; } else if(str[pos] == 'Z') { deltaZ--; } str[pos] = ch; if(str[pos] == 'C') { deltaC++; } else if(str[pos] == 'Z') { deltaZ++; } c += deltaC; z += deltaZ; if(pos%2==0) { cp += deltaC; zp += deltaZ; } else { cn += deltaC; zn += deltaZ; } N = n-c-z; ret = 0; for(int i=0;i<=2;i++) { if(correct(c+i)) { ret = (ret + sum[N][i]) % MOD; } } cout << dec(ret) << "\n"; } return 0; } |