#include "bits/stdc++.h" using namespace std; #define rep(i,a,b) for(int i=(a); i<(b); ++i) #define all(x) x.begin(),x.end() #define sz(x) int(x.size()) typedef long long ll; typedef long long ull; typedef vector<int> vi; typedef vector<vi> vvi; const int nchar = 2; const int B = nchar+1; const ull mod = 998244353; ull mul(ull a, ull b){ return a*b%mod; } // van I naar J struct matrix{ int M[B][B]; matrix() { rep(i,0,B) rep(j,0,B) M[i][j] = 0; rep(i,0,B) M[i][i] = 1; } void initAll(int pos){ rep(i,0,B) rep(j,0,B) M[i][j] = 0; M[B-1][B-1] = 2; // naar zelf rep(i,0,nchar) M[i][i] = 1; M[pos][pos] = 0; M[B-1][pos] = 1; M[pos][B-1] = -1; } void init0(int pos){ rep(i,0,B) rep(j,0,B) M[i][j] = 0; rep(i,0,B) rep(j,0,B) M[i][j] = 0; M[B-1][B-1] = 2; // naar zelf rep(i,0,nchar) M[i][i] = 1; M[pos][pos] = 0; M[B-1][pos] = 1; M[pos][B-1] = -2; } ull getAns(){ return M[B-1][B-1]; } matrix operator*(const matrix& b){ matrix res = matrix(); rep(i,0,B) rep(j,0,B){ ull sm = 0; rep(k,0,B) { sm += (ull)M[i][k]*b.M[k][j]; } res.M[i][j] = sm % mod;; } return res; } void print(){ rep(i,0,B){ if (i%nchar == 0 and i) cout << string(2+B*5, '-') << '\n'; rep(j,0,B) { if (j%nchar==0 and j) cout << '|'; cout << string(4-sz(to_string(M[j][i])),' ') << to_string(M[j][i]) << ' '; } cout << '\n'; }cout << endl; } }; int brute(string a){ map<string,int> mp; rep(i,0,1<<sz(a)){ string cur; rep(j,0,sz(a)) if (i&(1<<j)){ cur += a[j]; } mp[cur]++; } int ans = 0; for(auto [k,v] : mp){ ans += v>1; } return ans; } // alle a -> b maakt niks uit -> 2^b // niet alle a-> alle b's geforceerd in de gaps // // [2,4,6,9,14,22,35,56,90,145] // a[i] = a[i-1] + a[i-2] - 1 for i > const int N = 100000; ll tab[N]; struct TreeAll { typedef matrix T; T f(T& a, T& b) { return a*b; } // (any associative fn) vector<T> s; int n; TreeAll(int n = 0) : s(2*n, matrix()), n(n) {} void update(int i, int pos) { for (s[i += n].initAll(pos); i /= 2;) s[i] = f(s[i * 2], s[i * 2 + 1]); } T query(int b, int e) { // query [b, e) T ra = matrix(), rb = matrix(); for (b += n, e += n; b < e; b /= 2, e /= 2) { if (b % 2) ra = f(ra, s[b++]); if (e % 2) rb = f(s[--e], rb); } return f(ra, rb); } }; struct Tree0 { typedef matrix T; T f(T& a, T& b) { return a*b; } // (any associative fn) vector<T> s; int n; Tree0(int n = 0) : s(2*n, matrix()), n(n) {} void update(int i, int pos) { for (s[i += n].init0(pos); i /= 2;) s[i] = f(s[i * 2], s[i * 2 + 1]); } T query(int b, int e) { // query [b, e) T ra = matrix(), rb = matrix(); for (b += n, e += n; b < e; b /= 2, e /= 2) { if (b % 2) ra = f(ra, s[b++]); if (e % 2) rb = f(s[--e], rb); } return f(ra, rb); } }; int main(){ // matrix mat; // mat.init(3); // mat.print(); cin.tie(NULL),cin.sync_with_stdio(false); tab[0] = 1; tab[1] = 2; tab[2] = 4; tab[3] = 6; tab[4] = 9; rep(i,5,N){ tab[i] = (tab[i-1] + tab[i-2] - 1)%mod; if (tab[i] < 0) tab[i] += mod; } int n,q; cin >> n >> q; TreeAll segAll(n); // Tree0 seg0(n); string s; cin >> s; // count initial number of blocks int blocks = 1; rep(i,1,n){ blocks += s[i] != s[i-1]; } // rep(i,0,n) { segAll.update(i,s[i]-'a'); } cout << (segAll.query(0,n).getAns() + mod - tab[blocks])%mod << '\n'; while(q--){ int i; cin >> i; --i; char c; cin >> c; int pos = c-'a'; if (c != s[i]){ segAll.update(i,pos); // update number of blocks if (i==0){ if (s[i] != s[i+1]){ blocks -= 1; }else{ blocks += 1; } }else if (i+1==n){ if (s[i] != s[i-1]){ blocks -= 1; }else{ blocks += 1; } }else{ if (s[i-1] == s[i+1]){ if (s[i] != s[i+1]){ blocks -= 2; }else{ blocks += 2; } } } s[i] = c; } cout << (segAll.query(0,n).getAns() + mod - tab[blocks])%mod << '\n'; } // cout << brute(s) << '\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 | #include "bits/stdc++.h" using namespace std; #define rep(i,a,b) for(int i=(a); i<(b); ++i) #define all(x) x.begin(),x.end() #define sz(x) int(x.size()) typedef long long ll; typedef long long ull; typedef vector<int> vi; typedef vector<vi> vvi; const int nchar = 2; const int B = nchar+1; const ull mod = 998244353; ull mul(ull a, ull b){ return a*b%mod; } // van I naar J struct matrix{ int M[B][B]; matrix() { rep(i,0,B) rep(j,0,B) M[i][j] = 0; rep(i,0,B) M[i][i] = 1; } void initAll(int pos){ rep(i,0,B) rep(j,0,B) M[i][j] = 0; M[B-1][B-1] = 2; // naar zelf rep(i,0,nchar) M[i][i] = 1; M[pos][pos] = 0; M[B-1][pos] = 1; M[pos][B-1] = -1; } void init0(int pos){ rep(i,0,B) rep(j,0,B) M[i][j] = 0; rep(i,0,B) rep(j,0,B) M[i][j] = 0; M[B-1][B-1] = 2; // naar zelf rep(i,0,nchar) M[i][i] = 1; M[pos][pos] = 0; M[B-1][pos] = 1; M[pos][B-1] = -2; } ull getAns(){ return M[B-1][B-1]; } matrix operator*(const matrix& b){ matrix res = matrix(); rep(i,0,B) rep(j,0,B){ ull sm = 0; rep(k,0,B) { sm += (ull)M[i][k]*b.M[k][j]; } res.M[i][j] = sm % mod;; } return res; } void print(){ rep(i,0,B){ if (i%nchar == 0 and i) cout << string(2+B*5, '-') << '\n'; rep(j,0,B) { if (j%nchar==0 and j) cout << '|'; cout << string(4-sz(to_string(M[j][i])),' ') << to_string(M[j][i]) << ' '; } cout << '\n'; }cout << endl; } }; int brute(string a){ map<string,int> mp; rep(i,0,1<<sz(a)){ string cur; rep(j,0,sz(a)) if (i&(1<<j)){ cur += a[j]; } mp[cur]++; } int ans = 0; for(auto [k,v] : mp){ ans += v>1; } return ans; } // alle a -> b maakt niks uit -> 2^b // niet alle a-> alle b's geforceerd in de gaps // // [2,4,6,9,14,22,35,56,90,145] // a[i] = a[i-1] + a[i-2] - 1 for i > const int N = 100000; ll tab[N]; struct TreeAll { typedef matrix T; T f(T& a, T& b) { return a*b; } // (any associative fn) vector<T> s; int n; TreeAll(int n = 0) : s(2*n, matrix()), n(n) {} void update(int i, int pos) { for (s[i += n].initAll(pos); i /= 2;) s[i] = f(s[i * 2], s[i * 2 + 1]); } T query(int b, int e) { // query [b, e) T ra = matrix(), rb = matrix(); for (b += n, e += n; b < e; b /= 2, e /= 2) { if (b % 2) ra = f(ra, s[b++]); if (e % 2) rb = f(s[--e], rb); } return f(ra, rb); } }; struct Tree0 { typedef matrix T; T f(T& a, T& b) { return a*b; } // (any associative fn) vector<T> s; int n; Tree0(int n = 0) : s(2*n, matrix()), n(n) {} void update(int i, int pos) { for (s[i += n].init0(pos); i /= 2;) s[i] = f(s[i * 2], s[i * 2 + 1]); } T query(int b, int e) { // query [b, e) T ra = matrix(), rb = matrix(); for (b += n, e += n; b < e; b /= 2, e /= 2) { if (b % 2) ra = f(ra, s[b++]); if (e % 2) rb = f(s[--e], rb); } return f(ra, rb); } }; int main(){ // matrix mat; // mat.init(3); // mat.print(); cin.tie(NULL),cin.sync_with_stdio(false); tab[0] = 1; tab[1] = 2; tab[2] = 4; tab[3] = 6; tab[4] = 9; rep(i,5,N){ tab[i] = (tab[i-1] + tab[i-2] - 1)%mod; if (tab[i] < 0) tab[i] += mod; } int n,q; cin >> n >> q; TreeAll segAll(n); // Tree0 seg0(n); string s; cin >> s; // count initial number of blocks int blocks = 1; rep(i,1,n){ blocks += s[i] != s[i-1]; } // rep(i,0,n) { segAll.update(i,s[i]-'a'); } cout << (segAll.query(0,n).getAns() + mod - tab[blocks])%mod << '\n'; while(q--){ int i; cin >> i; --i; char c; cin >> c; int pos = c-'a'; if (c != s[i]){ segAll.update(i,pos); // update number of blocks if (i==0){ if (s[i] != s[i+1]){ blocks -= 1; }else{ blocks += 1; } }else if (i+1==n){ if (s[i] != s[i-1]){ blocks -= 1; }else{ blocks += 1; } }else{ if (s[i-1] == s[i+1]){ if (s[i] != s[i+1]){ blocks -= 2; }else{ blocks += 2; } } } s[i] = c; } cout << (segAll.query(0,n).getAns() + mod - tab[blocks])%mod << '\n'; } // cout << brute(s) << '\n'; } /* */ |