#include<bits/stdc++.h> using namespace std; using LL=long long; #define FOR(i,l,r) for(int i=(l);i<=(r);++i) #define REP(i,n) FOR(i,0,(n)-1) #define ssize(x) int(x.size()) template<class A,class B>auto&operator<<(ostream&o,pair<A,B>p){return o<<'('<<p.first<<", "<<p.second<<')';} template<class T>auto operator<<(ostream&o,T x)->decltype(x.end(),o){o<<'{';int i=0;for(auto e:x)o<<(", ")+2*!i++<<e;return o<<'}';} #ifdef DEBUG #define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...)<<'\n';}(x) #else #define debug(...) {} #endif /* * Opis: Struktura do działań modulo * Czas: O(1), dzielenie O(\log mod) * Użycie: Ustaw modulo w ostatniej linii. Jeśli modulo nie jest const, usuń pierwszy wiersz i zadeklaruj mod */ template<int mod> struct modular { int val; modular() { val = 0; } modular(const LL& v) { val = int((-mod <= v && v < mod) ? (int) v : v % mod); if(val < 0) val += mod; } int to_int() { return val; } friend ostream& operator<<(ostream &os, const modular &a) { #ifdef DEBUG constexpr int mx = 1024; for(int y = 1; y <= mx; ++y) if(a * y <= mx) return os << (a * y).val << '/' << y; else if(mod - a * y <= mx) return os << '-' << (mod - a * y).val << '/' << y; #endif return os << a.val; } friend istream& operator>>(istream &is, modular &a) { return is >> a.val; } friend bool operator==(const modular &a, const modular &b) { return a.val == b.val; } friend bool operator!=(const modular &a, const modular &b) { return !(a == b); } friend bool operator<(const modular &a, const modular &b) { return a.val < b.val; } friend bool operator<=(const modular &a, const modular &b) { return a.val <= b.val; } modular operator-() const { return modular(-val); } modular& operator+=(const modular &m) { if((val += m.val) >= mod) val -= mod; return *this; } modular& operator-=(const modular &m) { if((val -= m.val) < 0) val += mod; return *this; } modular& operator*=(const modular &m) { val = int((LL) val * m.val % mod); return *this; } friend modular qpow(modular a, LL n) { if(n == 0) return 1; if(n % 2 == 1) return qpow(a, n - 1) * a; return qpow(a * a, n / 2); } friend modular inv(const modular &a) { assert(a != 0); return qpow(a, mod - 2); } modular& operator/=(const modular &m) { return (*this) *= inv(m); } modular operator++(int) { modular res = (*this); (*this) += 1; return res; } friend modular operator+(modular a, const modular &b) { return a += b; } friend modular operator-(modular a, const modular &b) { return a -= b; } friend modular operator*(modular a, const modular &b) { return a *= b; } friend modular operator/(modular a, const modular &b) { return a /= b; } }; using mint = modular<int(1e9 + 7)>; struct BinomCoeff { vector<mint> fac, rev; BinomCoeff(int n) { fac = rev = vector<mint>(n + 1, 1); FOR(i, 1, n) fac[i] = fac[i - 1] * i; rev[n] = 1 / fac[n]; for(int i = n; i >= 1; i--) rev[i - 1] = rev[i] * i; } mint operator()(int n, int k) { return fac[n] * rev[n - k] * rev[k]; } }; constexpr int sz = 3; struct Matrix { array<array<mint, sz>, sz> t; Matrix(bool id = false) { REP (i, sz) REP (j, sz) t[i][j] = 0; if (id) REP (i, sz) t[i][i] = 1; } Matrix(array<array<mint, sz>, sz> v) : t(v) {} array<mint, sz> &operator[](int x) { return t[x]; } friend Matrix operator*(Matrix p, Matrix q) { Matrix ret; REP (k, sz) REP (i, sz) REP (j, sz) ret[i][j] += p[i][k] * q[k][j]; return ret; } friend Matrix operator+(Matrix p, Matrix q) { REP (i, sz) REP (j, sz) p[i][j] += q[i][j]; return p; } }; int main() { cin.tie(0)->sync_with_stdio(0); int n, q; cin >> n >> q; if (n == 1) { char c; cin >> c; cout << 1 + (c == 'N') << '\n'; REP (i, q) { cin >> n >> c; cout << 1 + (c == 'N') << '\n'; } return 0; } int leaves = 1; while (leaves < n) leaves <<= 1; vector st(leaves << 1, Matrix(true)); auto give_mat = [&](char c) { Matrix ret; if (c != 'Z') REP (i, 3) ret[i][i]++; if (c != 'C') REP (i, 3) ret[i][(i + 1) % 3]++; return ret; }; vector<char> t(n); vector zli(2, vector (2, 0)); REP (i, n) { cin >> t[i]; if (t[i] == 'Z') ++zli[i % 2][0]; if (t[i] == 'C') ++zli[i % 2][1]; st[i + leaves] = give_mat(t[i]); } auto good = [&](int x, int y) { while (x) { x = (x + 1) % 3; y = (y + 2) % 3; } return x != y; }; auto update = [&](int x) { st[x] = st[x * 2] * st[x * 2 + 1]; }; auto ins = [&](int x, char c) { if (t[x] == 'Z') --zli[x % 2][0]; if (t[x] == 'C') --zli[x % 2][1]; t[x] = c; if (t[x] == 'Z') ++zli[x % 2][0]; if (t[x] == 'C') ++zli[x % 2][1]; x += leaves; st[x] = give_mat(c); x >>= 1; while (x) { update(x); x >>= 1; } }; auto get_answer = [&]() { mint ans = 0; REP (i, 3) if (good(i, n % 3)) ans += st[1][0][i]; return ans - (zli[0][0] == 0 && zli[1][1] == 0 && good(((n + 1) / 2) % 3, n % 3)) - (zli[0][1] == 0 && zli[1][0] == 0 && good(((n / 2) % 3), n % 3)); }; for (int i = leaves - 1; i; --i) update(i); cout << get_answer() << '\n'; REP (i, q) { int x; char c; cin >> x >> c; --x; ins(x, c); cout << get_answer() << '\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 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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | #include<bits/stdc++.h> using namespace std; using LL=long long; #define FOR(i,l,r) for(int i=(l);i<=(r);++i) #define REP(i,n) FOR(i,0,(n)-1) #define ssize(x) int(x.size()) template<class A,class B>auto&operator<<(ostream&o,pair<A,B>p){return o<<'('<<p.first<<", "<<p.second<<')';} template<class T>auto operator<<(ostream&o,T x)->decltype(x.end(),o){o<<'{';int i=0;for(auto e:x)o<<(", ")+2*!i++<<e;return o<<'}';} #ifdef DEBUG #define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...)<<'\n';}(x) #else #define debug(...) {} #endif /* * Opis: Struktura do działań modulo * Czas: O(1), dzielenie O(\log mod) * Użycie: Ustaw modulo w ostatniej linii. Jeśli modulo nie jest const, usuń pierwszy wiersz i zadeklaruj mod */ template<int mod> struct modular { int val; modular() { val = 0; } modular(const LL& v) { val = int((-mod <= v && v < mod) ? (int) v : v % mod); if(val < 0) val += mod; } int to_int() { return val; } friend ostream& operator<<(ostream &os, const modular &a) { #ifdef DEBUG constexpr int mx = 1024; for(int y = 1; y <= mx; ++y) if(a * y <= mx) return os << (a * y).val << '/' << y; else if(mod - a * y <= mx) return os << '-' << (mod - a * y).val << '/' << y; #endif return os << a.val; } friend istream& operator>>(istream &is, modular &a) { return is >> a.val; } friend bool operator==(const modular &a, const modular &b) { return a.val == b.val; } friend bool operator!=(const modular &a, const modular &b) { return !(a == b); } friend bool operator<(const modular &a, const modular &b) { return a.val < b.val; } friend bool operator<=(const modular &a, const modular &b) { return a.val <= b.val; } modular operator-() const { return modular(-val); } modular& operator+=(const modular &m) { if((val += m.val) >= mod) val -= mod; return *this; } modular& operator-=(const modular &m) { if((val -= m.val) < 0) val += mod; return *this; } modular& operator*=(const modular &m) { val = int((LL) val * m.val % mod); return *this; } friend modular qpow(modular a, LL n) { if(n == 0) return 1; if(n % 2 == 1) return qpow(a, n - 1) * a; return qpow(a * a, n / 2); } friend modular inv(const modular &a) { assert(a != 0); return qpow(a, mod - 2); } modular& operator/=(const modular &m) { return (*this) *= inv(m); } modular operator++(int) { modular res = (*this); (*this) += 1; return res; } friend modular operator+(modular a, const modular &b) { return a += b; } friend modular operator-(modular a, const modular &b) { return a -= b; } friend modular operator*(modular a, const modular &b) { return a *= b; } friend modular operator/(modular a, const modular &b) { return a /= b; } }; using mint = modular<int(1e9 + 7)>; struct BinomCoeff { vector<mint> fac, rev; BinomCoeff(int n) { fac = rev = vector<mint>(n + 1, 1); FOR(i, 1, n) fac[i] = fac[i - 1] * i; rev[n] = 1 / fac[n]; for(int i = n; i >= 1; i--) rev[i - 1] = rev[i] * i; } mint operator()(int n, int k) { return fac[n] * rev[n - k] * rev[k]; } }; constexpr int sz = 3; struct Matrix { array<array<mint, sz>, sz> t; Matrix(bool id = false) { REP (i, sz) REP (j, sz) t[i][j] = 0; if (id) REP (i, sz) t[i][i] = 1; } Matrix(array<array<mint, sz>, sz> v) : t(v) {} array<mint, sz> &operator[](int x) { return t[x]; } friend Matrix operator*(Matrix p, Matrix q) { Matrix ret; REP (k, sz) REP (i, sz) REP (j, sz) ret[i][j] += p[i][k] * q[k][j]; return ret; } friend Matrix operator+(Matrix p, Matrix q) { REP (i, sz) REP (j, sz) p[i][j] += q[i][j]; return p; } }; int main() { cin.tie(0)->sync_with_stdio(0); int n, q; cin >> n >> q; if (n == 1) { char c; cin >> c; cout << 1 + (c == 'N') << '\n'; REP (i, q) { cin >> n >> c; cout << 1 + (c == 'N') << '\n'; } return 0; } int leaves = 1; while (leaves < n) leaves <<= 1; vector st(leaves << 1, Matrix(true)); auto give_mat = [&](char c) { Matrix ret; if (c != 'Z') REP (i, 3) ret[i][i]++; if (c != 'C') REP (i, 3) ret[i][(i + 1) % 3]++; return ret; }; vector<char> t(n); vector zli(2, vector (2, 0)); REP (i, n) { cin >> t[i]; if (t[i] == 'Z') ++zli[i % 2][0]; if (t[i] == 'C') ++zli[i % 2][1]; st[i + leaves] = give_mat(t[i]); } auto good = [&](int x, int y) { while (x) { x = (x + 1) % 3; y = (y + 2) % 3; } return x != y; }; auto update = [&](int x) { st[x] = st[x * 2] * st[x * 2 + 1]; }; auto ins = [&](int x, char c) { if (t[x] == 'Z') --zli[x % 2][0]; if (t[x] == 'C') --zli[x % 2][1]; t[x] = c; if (t[x] == 'Z') ++zli[x % 2][0]; if (t[x] == 'C') ++zli[x % 2][1]; x += leaves; st[x] = give_mat(c); x >>= 1; while (x) { update(x); x >>= 1; } }; auto get_answer = [&]() { mint ans = 0; REP (i, 3) if (good(i, n % 3)) ans += st[1][0][i]; return ans - (zli[0][0] == 0 && zli[1][1] == 0 && good(((n + 1) / 2) % 3, n % 3)) - (zli[0][1] == 0 && zli[1][0] == 0 && good(((n / 2) % 3), n % 3)); }; for (int i = leaves - 1; i; --i) update(i); cout << get_answer() << '\n'; REP (i, q) { int x; char c; cin >> x >> c; --x; ins(x, c); cout << get_answer() << '\n'; } } |