#include <iostream> const int64_t MOD = 1000000007; const int M[5][5] = { {1, 4, 0, 0, 0}, {4, 0, 1, 1, 1}, {0, 1, 2, 4, 4}, {0, 1, 4, 3, 4}, {0, 1, 4, 4, 4}, }; const int64_t A[4][2] = { {1, 0}, {0, 1}, {1, 1}, {0, 0} }; int AA(char c) { switch (c) { case 'Z': return 0; case 'C': return 1; case 'N': return 2; } return 3; } struct V { int64_t x[5]; static V make(const int64_t a[2], const int64_t b[2]) { V v; // std::clog << a[0] << " " << a[1] << " | " << b[0] << " " << b[1] << std::endl; v.x[0] = a[0]*b[0]%MOD; v.x[1] = a[1]*b[1]%MOD; v.x[2] = a[0]*b[1]%MOD; v.x[3] = a[1]*b[0]%MOD; v.x[4] = 0; return v; } V operator+(const V& w) const { const V& v = *this; if (w.x[0] == -1) return v; if (v.x[0] == -1) return w; V u; for (int i=0;i<5;++i) u.x[i] = 0; for (int i=0;i<5;++i) { for (int j=0;j<5;++j) { int64_t& xx = u.x[M[i][j]]; xx = (xx + v.x[i]*w.x[j]) % MOD; } } return u; } int64_t C() const { return (x[0]+x[1])%MOD; } int64_t C(char c) const { if (c=='Z') return (x[1]+x[3]+x[4])%MOD; else if (c=='C') return (x[0]+x[2]+x[4])%MOD; else if (c=='N') return (C('Z')+C('C'))%MOD; else return 0; } }; std::ostream& operator<<(std::ostream& out, V v) { out << "{" << v.x[0] << ", " << v.x[1] << ", " << v.x[2] << ", " << v.x[3] << ", " << v.x[4] << "}"; return out; } const int MAX = 1<<18; // const int MAX = 1<<4; V D[MAX]; std::string S; int n,q; V make(char a, char b) { return V::make(A[AA(a)], A[AA(b)]); } void init() { for (int i=0;i<MAX;i++) D[i].x[0] = -1; for (int i=0;i+1<n;i+=2) { D[MAX/2+i/2] = make(S[i], S[i+1]); // std::clog << MAX/2+i/2 << " :: " << D[MAX/2+i/2]<<std::endl; } for (int i=MAX/2-1;i>0;--i) { D[i] = D[2*i]+D[2*i+1]; // std::clog << i << " :: " << D[i]<<std::endl; } } void update(int i) { i = i/2*2; D[MAX/2+i/2] = make(S[i], S[i+1]); i = MAX/2+i/2; // std::clog << i << " :: " << D[i]<<std::endl; while (i>1) { i/=2; D[i] = D[2*i]+D[2*i+1]; // std::clog << i << " :: " << D[i]<<std::endl; } // init(); } /* 5 1 1 | 1 0 8 :: {1, 0, 0, 1, 0} 1 1 | 0 1 9 :: {0, 1, 1, 0, 0} 7 :: {-1, 0, 0, 0, 0} 6 :: {-1, 0, 0, 0, 0} 5 :: {-1, 0, 0, 0, 0} 4 :: {1, 1, 0, 0, 2} 3 :: {-1, 0, 0, 0, 0} 2 :: {1, 1, 0, 0, 2} 1 :: {1, 1, 0, 0, 2} */ int64_t sol() { if (n%2 == 0) return D[1].C(); else return D[1].C(S[n-1]); } int main() { std::ios_base::sync_with_stdio(0); std::cin >> n >> q; std::cin >> S; init(); std::cout << sol() << "\n"; for (int i=0;i<q;++i) { int k; char c; std::cin >> k >> c; --k; S[k] = c; if (n%2==0 || k!=n-1) update(k); std::cout << sol() << "\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 | #include <iostream> const int64_t MOD = 1000000007; const int M[5][5] = { {1, 4, 0, 0, 0}, {4, 0, 1, 1, 1}, {0, 1, 2, 4, 4}, {0, 1, 4, 3, 4}, {0, 1, 4, 4, 4}, }; const int64_t A[4][2] = { {1, 0}, {0, 1}, {1, 1}, {0, 0} }; int AA(char c) { switch (c) { case 'Z': return 0; case 'C': return 1; case 'N': return 2; } return 3; } struct V { int64_t x[5]; static V make(const int64_t a[2], const int64_t b[2]) { V v; // std::clog << a[0] << " " << a[1] << " | " << b[0] << " " << b[1] << std::endl; v.x[0] = a[0]*b[0]%MOD; v.x[1] = a[1]*b[1]%MOD; v.x[2] = a[0]*b[1]%MOD; v.x[3] = a[1]*b[0]%MOD; v.x[4] = 0; return v; } V operator+(const V& w) const { const V& v = *this; if (w.x[0] == -1) return v; if (v.x[0] == -1) return w; V u; for (int i=0;i<5;++i) u.x[i] = 0; for (int i=0;i<5;++i) { for (int j=0;j<5;++j) { int64_t& xx = u.x[M[i][j]]; xx = (xx + v.x[i]*w.x[j]) % MOD; } } return u; } int64_t C() const { return (x[0]+x[1])%MOD; } int64_t C(char c) const { if (c=='Z') return (x[1]+x[3]+x[4])%MOD; else if (c=='C') return (x[0]+x[2]+x[4])%MOD; else if (c=='N') return (C('Z')+C('C'))%MOD; else return 0; } }; std::ostream& operator<<(std::ostream& out, V v) { out << "{" << v.x[0] << ", " << v.x[1] << ", " << v.x[2] << ", " << v.x[3] << ", " << v.x[4] << "}"; return out; } const int MAX = 1<<18; // const int MAX = 1<<4; V D[MAX]; std::string S; int n,q; V make(char a, char b) { return V::make(A[AA(a)], A[AA(b)]); } void init() { for (int i=0;i<MAX;i++) D[i].x[0] = -1; for (int i=0;i+1<n;i+=2) { D[MAX/2+i/2] = make(S[i], S[i+1]); // std::clog << MAX/2+i/2 << " :: " << D[MAX/2+i/2]<<std::endl; } for (int i=MAX/2-1;i>0;--i) { D[i] = D[2*i]+D[2*i+1]; // std::clog << i << " :: " << D[i]<<std::endl; } } void update(int i) { i = i/2*2; D[MAX/2+i/2] = make(S[i], S[i+1]); i = MAX/2+i/2; // std::clog << i << " :: " << D[i]<<std::endl; while (i>1) { i/=2; D[i] = D[2*i]+D[2*i+1]; // std::clog << i << " :: " << D[i]<<std::endl; } // init(); } /* 5 1 1 | 1 0 8 :: {1, 0, 0, 1, 0} 1 1 | 0 1 9 :: {0, 1, 1, 0, 0} 7 :: {-1, 0, 0, 0, 0} 6 :: {-1, 0, 0, 0, 0} 5 :: {-1, 0, 0, 0, 0} 4 :: {1, 1, 0, 0, 2} 3 :: {-1, 0, 0, 0, 0} 2 :: {1, 1, 0, 0, 2} 1 :: {1, 1, 0, 0, 2} */ int64_t sol() { if (n%2 == 0) return D[1].C(); else return D[1].C(S[n-1]); } int main() { std::ios_base::sync_with_stdio(0); std::cin >> n >> q; std::cin >> S; init(); std::cout << sol() << "\n"; for (int i=0;i<q;++i) { int k; char c; std::cin >> k >> c; --k; S[k] = c; if (n%2==0 || k!=n-1) update(k); std::cout << sol() << "\n"; } return 0; } |