[BZOJ-3160]万径人踪灭

Description

给你一个只含a,b的字符串,让你选取子序列使得:

  1. 位置和字符都关于每条对称轴对称。
  2. 不能是连续的一段。
    求这样的子序列的个数。

Solution

我们发现不连续的并不好求,那么我们可以通过满足第一个条件的个数减去所有的回文串来得到答案。
求回文串的个数可以用Manacher来做。
那么如何算出所有满足第一个条件的子序列个数呢?
我们假设F[i]表示以i这里为对称轴,2边字符相等,位置相对的对数。
那么满足第一个条件的子序列个数ans = sigma(2 ^ F[i] - 1)
问题就转化为如何求F[i]
我们发现这样能对F[i]作出贡献的对(x,y)都有共同点:x + y = 2 i
我们可以在每位a上存1,每位b上存0,这样子只有2个a才能产生贡献,
那么一对a对F[i]的贡献就是这个数组的平方后2
i位上的系数。
同理也可以求出一对b对F[i]的贡献。
总时间复杂度为O(n ^ 2)

Notice

注意每一对会对F数组产生2次贡献,所以最后计算F的时候要除以2。

Code

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);
}
文章目录
  1. 1. Description
  2. 2. Solution
  3. 3. Notice
  4. 4. Code