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
| #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; using namespace std; using ull = uint64_t; const int N = 3e5 + 5; const int mod = 998244353, mod2 = 1e9 + 9, base = 19260817; int kase; int n, fac[N<<1], ifac[N<<1], cnt[N<<1], ans, tot; int HSH, HSH2;
gp_hash_table<ull, int> id; gp_hash_table<ull, bool> vis;
char s[N]; int pw[N<<1], pw2[N<<1]; int hsh[N], hsh2[N]; void Add(int &x, int y) { x += y; if(x>=mod) x -= mod; } int Pow(int a, int b) { int ans = 1; while(b) { if(b&1) ans = 1ll*ans*a%mod; a = 1ll*a*a%mod; b >>= 1; } return ans; } void pre(int n) { pw[0] = pw2[0] = fac[0] = 1; for(int i=1; i<=2*n; i++) { pw[i] = 1ll*pw[i-1]*base%mod; pw2[i] = 1ll*pw2[i-1]*base%mod2; fac[i] = 1ll*fac[i-1]*i%mod; } ifac[n] = Pow(fac[n], mod-2); for(int i=n-1; i>=0; i--) ifac[i] = 1ll*ifac[i+1]*(i+1)%mod; } void init() { for(int i=1; i<=n; i++) { hsh[i] = (1ll*hsh[i-1]*base%mod + s[i])%mod; hsh2[i] = (1ll*hsh2[i-1]*base%mod2 + s[i])%mod2; } } inline int Sub(int x, int y) { x -= y; if(x<0) x += mod; return x; } inline int Sub2(int x, int y) { x -= y; if(x<0) x += mod2; return x; } int get(int l, int r) { return Sub(hsh[r], 1ll*hsh[l-1]*pw[r-l+1]%mod); } int get2(int l, int r) { return Sub2(hsh2[r], 1ll*hsh2[l-1]*pw2[r-l+1]%mod2); } void upd(int l, int r, int v) { ull hv = (ull)get(l, r)<<32|get2(l, r), p = 0; auto it = id.find(hv); if(it==id.end()) p = id[hv] = ++tot; else p = it->second; HSH ^= 1ll*pw[p]*cnt[p]%mod; HSH2 ^= 1ll*pw2[p]*cnt[p]%mod2; ans = 1ll*ans*fac[cnt[p]]%mod*ifac[cnt[p] + v]%mod; cnt[p] += v; HSH ^= 1ll*pw[p]*cnt[p]%mod; HSH2 ^= 1ll*pw2[p]*cnt[p]%mod2; } void clear(gp_hash_table<ull, int> &table) { gp_hash_table<ull, int> tmp; table.swap(tmp); } void clear(gp_hash_table<ull, bool> &table) { gp_hash_table<ull, bool> tmp; table.swap(tmp); } int work(int d) { for(int i=1; i<=tot; i++) cnt[i] = 0; clear(id); clear(vis); tot = HSH = HSH2 = 0; int sum = 0; ans = fac[n/d]; for(int i=n%d+1; i<=n; i+=d) upd(i, i+d-1, 1); Add(sum, ans); vis.insert({(ull)HSH<<32|HSH2, 1}); if(n%d) { int m = n/d*d; for(int i=1; i<m; i+=d) { upd(i, i+d-1, 1); upd(i+n%d, i+n%d+d-1, -1); if(vis.find((ull)HSH<<32|HSH2)==vis.end()) Add(sum, ans), vis.insert({(ull)HSH<<32|HSH2, 1}); } }
return sum; } void solve() { scanf("%s", s+1); n = strlen(s+1); init(); int res = 0; for(int d=1; d<=n; d++) Add(res, work(d)); printf("Case #%d: %d\n", ++kase, res); } int main() { pre(N-5); int _; scanf("%d", &_); while(_--) solve(); return 0; }
|