链接
题意
给出一个长度不超过1e6的字符串,求其中前K(<=1e12)长的奇数回文串长度的乘积,结果对19930726取余。
思路
这是一个回文串计数问题,先跑manacher对每个位置求出最长回文半径,由于只考虑奇数长度回文串,不需要在字符两端加’#’字符。由字符串的单调性,以某个字符为回文中心的最长回文半径R[i]会给[1, R[i]]区间的回文串个数提供+1的增益。之后用差分数组+前缀和便可求出所有长度的回文串个数。之后扫一遍,快速幂得出答案。
代码
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
typedef long long lint;
lint mod;
lint quick_pow(lint a, lint n)
{
if(!n) return 1 % mod;
lint tmp = quick_pow(a, n >> 1);
tmp = tmp * tmp % mod;
if(n & 1) tmp = tmp * a % mod;
return tmp;
}
#define maxn (1000010)
char s[maxn];
int R[maxn];
void manacher(char s[], int n, int *R)
{
int p = 0, mx = 0;
R[0] = 1;
for(int i = 1; i < n; i++)
{
if(i < mx)
R[i] = min(R[2*p - i], mx - i);
else
R[i] = 1;
while(s[i + R[i]] == s[i - R[i]])
R[i]++;
if(i + R[i] > mx)
p = i, mx = i + R[i];
}
}
lint d[maxn], cnt[maxn];
int main()
{
lint n, K;
cin >> n >> K;
scanf("%s", s + 1);
s[0] = '$';
manacher(s, n + 1, R);
for(int i = 1; i <= n; i++)
{
d[1]++;
d[R[i] + 1]--;
}
for(int i = 1; i <= n; i++)
{
cnt[i] = cnt[i-1] + d[i];
}
mod = 19930726;
lint ans = 1;
for(int i = n; i; i--) if(cnt[i])
{
if(cnt[i] >= K) { ans = ans * quick_pow(i*2 - 1, K) % mod; K = 0; break; }
else
{
ans = ans * quick_pow(i*2 - 1, cnt[i]) % mod;
K -= cnt[i];
}
}
if(K) cout << -1 << endl;
else cout << ans << endl;
return 0;
}