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
| #include<cmath> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<vector> using namespace std; #define sqz main #define ll long long #define rep(i, a, b) for (int i = (a); i <= (b); i++) #define per(i, a, b) for (int i = (a); i >= (b); i--) #define Rep(i, a, b) for (int i = (a); i < (b); i++) #define travel(i, u) for (int i = head[u]; ~i; i = edge[i].next)
const ll INF = 1e9, Mo = INF + 7; const int N = 263000; const double eps = 1e-6, pi = acos(-1.0); ll read() { ll x = 0; int zf = 1; char ch = getchar(); while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar(); if (ch == '-') zf = -1, ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * zf; } void write(ll y) { if (y < 0) putchar('-'), y = -y; if (y > 9) write(y / 10); putchar(y % 10 + '0'); }
char st[N + 5], S[N + 5]; int Ans[N + 5], X[N + 5], Y[N + 5], Rev[N + 5]; int t; void Manacher(char *S, int len, int *Rad) { for (int i = 0, j = 0, k; i < len; i += k) { while (S[i - j - 1] == S[i + j + 1]) j++; Rad[i] = j; for (k = 1; k <= j && j - k != Rad[i - k]; k++) Rad[i + k] = min(j - k, Rad[i - k]); j = max(j - k, 0); } } ll pow(ll a, ll b, ll Mo) { ll ans = 1; while (b) { if (b & 1) ans = ans * a % Mo; a = a * a % Mo; b >>= 1; } return ans; }
struct Complex { double real, imagine; Complex (double real = 0, double imagine = 0) : real(real), imagine(imagine) {}; Complex operator +(const Complex X) { return Complex(real + X.real, imagine + X.imagine); } Complex operator -(const Complex X) { return Complex(real - X.real, imagine - X.imagine); } Complex operator *(const Complex X) { return Complex(real * X.real - imagine * X.imagine, real * X.imagine + imagine * X.real); } }XX[N + 5], YY[N + 5]; void FFT(Complex *T, int n, int op) { Rep(i, 0, n) if (i < Rev[i]) swap(T[i], T[Rev[i]]); for (int Size = 1; Size < n; Size <<= 1) { Complex wn = Complex(cos(pi / Size), sin(pi / Size) * op); for (int L = 0; L < n; L += (Size << 1)) { Complex w = Complex(1, 0); Rep(R, L, L + Size) { Complex x = T[R], y = w * T[R + Size]; T[R] = x + y, T[R + Size] = x - y; w = w * wn; } } } } void Mul(int *T, int n) { int x = 2 * n; int l = 0; for (t = 1; t <= x; t <<= 1) l++; Rep(i, 0, t) Rev[i] = (Rev[i >> 1] >> 1) + ((i & 1) << l - 1); Rep(i, 0, n) XX[i] = YY[i] = Complex(T[i], 0); Rep(i, n, t) XX[i] = YY[i] = Complex(0, 0); FFT(XX, t, 1); FFT(YY, t, 1); Rep(i, 0, t) XX[i] = XX[i] * YY[i]; FFT(XX, t, -1); rep(i, 0, x) T[i] = (int)(XX[i].real / t + 0.5); }
int sqz() { scanf("%s", st); int len = strlen(st); st[len] = ')'; S[0] = '('; rep(i, 0, len) S[2 * i + 1] = '#', S[2 * (i + 1)] = st[i]; len = len * 2 + 3; Manacher(S, len, Ans); int ans1 = 0, ans2 = 0; Rep(i, 0, len) (ans1 += (Ans[i] + 1) / 2) %= Mo; len = strlen(st) - 1; Rep(i, 0, len) if (st[i] == 'a') X[i] = 1; else Y[i] = 1; Mul(X, len); Mul(Y, len); rep(i, 0, 2 * len) (ans2 += pow(2, (X[i] + 1) / 2 + (Y[i] + 1) / 2, Mo) - 1) %= Mo; printf("%d\n", (ans2 - ans1 + Mo) % Mo); }
|