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
| #include <bits/stdc++.h> #define IL inline namespace FIO { const int L = (1 << 20) + 5; int l = 0; char buf[L], out[L], *S, *E; #define gh() (S == E ? E = (S = buf) + fread(buf, 1, L, stdin), (S == E ? EOF : *S++) : *S++) IL void flus(){ fwrite(out, 1, l, stdout), l = 0; } IL void chk(){ if (l >= L - 5) flus(); } template<class T> IL void read(T &x) { char ch = gh(), t = 0; for (; ch < '0' || ch > '9'; ch = gh()) t |= ch == '-'; for (x = 0; ch >= '0' && ch <= '9'; ch = gh()) x = x * 10 + (ch ^ 48); if (t) x = -x; } IL void readc(char *x) { char ch = gh(); for (; ch < 'a' || ch > 'z'; ch = gh()); for (; ch >= 'a' && ch <= 'z'; ch = gh()) *x++ = ch; *x = '\0'; } IL void putc(char x){ out[l++] = x, chk(); } template<class T> IL void write(T x) { if (x < 0) putc('-'), x = -x; if (x > 9) write(x / 10); out[l++] = x % 10 + 48, chk(); } #undef gh } using FIO::flus; using FIO::read; using FIO::putc; using FIO::write; using FIO::readc; using LL = long long; const int N = (1 << 20) + 1, A = 30, FN = 14698342 + 100; int n, ne[N + 5], num[N + 5][A], ct, cnt[A], b[N + 5], fac[FN]; LL ans = 0; char s[N + 5]; IL LL ask(int l, int r, int up) { return num[r][up] - (l ? num[l - 1][up] : 0); } int main() { for (int i = 1; i < N; ++i) for (int j = 1; LL(i) * j < N; ++j) ++b[i * j]; for (int i = 2; i < N; ++i) b[i] += b[i - 1]; for (int i = N - 1; i; --i) b[i] = b[i - 1]; for (int i = 1; i < N; ++i) for (int j = 1; LL(i) * j < N; ++j) fac[++b[i * j]] = i; int T; for (read(T); T--; ) { readc(s + 1), n = strlen(s + 1); ne[1] = ans = 0; for (int i = 2, j = 0; i <= n; ++i) { while (j && s[i] != s[j + 1]) j = ne[j]; if (s[i] == s[j + 1]) ++j; ne[i] = j; } memset(num, 0, sizeof num); memset(cnt, 0, sizeof cnt), ct = 0; for (int i = 1; i <= n; ++i) { ((++cnt[s[i] - 'a']) & 1) ? ++ct :--ct; num[i][ct] = 1; } for (int i = 1; i <= n; ++i) for (int j = 1; j < 26; ++j) num[i][j] += num[i][j - 1]; for (int j = 0; j < 26; ++j) for (int i = 2; i <= n; ++i) num[i][j] += num[i - 1][j]; memset(cnt, 0, sizeof cnt), ct = 0; for (int i = n, len = i - 1, p, np, now, last, si; i >= 2; --i, --len) { ((++cnt[s[i] - 'a']) & 1) ? ++ct : --ct; if (len % (p = len - ne[len])) p = len; np = len / p, last = 0, si = b[np] - b[np - 1]; for (int j = b[np - 1] + 1, t = b[np]; j <= t; ++j) { now = fac[j]; ans += ask(last * p, now * p - 1, ct) * si; --si, last = now; } } write(ans), putc('\n'); } return flus(), 0; }
|