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
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll; typedef pair<int, int> pii; const int N = 2e6 + 10, mod = 998244353;
int si[N], sc[N], sp[N]; int sic[N], scp[N], spc[N]; int sicp[N], scpc[N]; int sicpc[N], u[N], v[N]; char s[N];
void solve() { int n, q; scanf("%d%d%s", &n, &q, s); ll x, a, b, p; scanf("%lld%lld%lld%lld", &x, &a, &b, &p); for(int i = 1; i <= q; i++){ x = (a * x + b) % p; u[i] = x % n; } for(int i = 1; i <= q; i++){ x = (a * x + b) % p; v[i] = x % n; if(u[i] > v[i]) swap(u[i], v[i]); u[i]++, v[i]++; } for(int i = 1; i <= n; i++){ (si[i] = si[i - 1] + (s[i - 1] == 'I')) %= mod; (sc[i] = sc[i - 1] + (s[i - 1] == 'C')) %= mod; (sp[i] = sp[i - 1] + (s[i - 1] == 'P')) %= mod; (sic[i] = sic[i - 1] + si[i - 1] * (s[i - 1] == 'C')) %= mod; (scp[i] = scp[i - 1] + sc[i - 1] * (s[i - 1] == 'P')) %= mod; (spc[i] = spc[i - 1] + sp[i - 1] * (s[i - 1] == 'C')) %= mod; (sicp[i] = sicp[i - 1] + sic[i - 1] * (s[i - 1] == 'P')) %= mod; (scpc[i] = scpc[i - 1] + scp[i - 1] * (s[i - 1] == 'C')) %= mod; (sicpc[i] = sicpc[i - 1] + sicp[i - 1] * (s[i - 1] == 'C')) %= mod; } int ans = 0; for(int i = 1; i <= q; i++){ int l = u[i], r = v[i]; ll res = sicpc[r] - sicpc[l - 1]; ll c = (sc[r] - sc[l - 1]); res -= c * sicp[l - 1] % mod; res %= mod; ll pc = 1ll * (spc[r] - spc[l - 1]) - c * sp[l - 1] % mod; pc %= mod; res = (res - pc * sic[l - 1] % mod) % mod; ll cpc = 1ll * (scpc[r] - scpc[l - 1]) - c * scp[l - 1] % mod - pc * sc[l - 1] % mod; cpc %= mod; res = (res - cpc * si[l - 1] % mod) % mod; if(res < 0) res += mod; ans = (res + ans) % mod; } printf("%d", ans); }
int main() { int T = 1; while (T--) solve(); return 0; }
|