题解
这题好坑啊。模数也十分诡异。
注意:
K≤1012
K
≤
10
12
,要开
long long
l
o
n
g
l
o
n
g
。
注意:只要奇数个数的回文串
注意:达不到
K
K
个输出
还得套个快速幂
代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e6+10,mod=19930726;
int p,n,ch[N][26],len[N],f[N],tot;
ll K,ans=1,t[N],cnt[N];
char s[N];
inline ll fp(ll a,ll b)
{
ll re=1;
for(;b;b>>=1,a=a*a%mod) if(b&1) re=re*a%mod;
return re;
}
inline int new_node(int m){len[tot]=m;return tot++;}
inline int get_fail(int x,int m)
{for(;s[m-len[x]]!=s[m+1];x=f[x]);return x;}
inline void insert(int alp,int m)
{
p=get_fail(p,m);
if(!ch[p][alp]){
int now=new_node(len[p]+2);
f[now]=ch[get_fail(f[p],m)][alp];
ch[p][alp]=now;
}
cnt[(p=ch[p][alp])]++;
}
int main(){
int i,j;
scanf("%d%lld%s",&n,&K,s+1);
new_node(0);new_node(-1);f[0]=1;
for(i=1;i<=n;++i) insert(s[i]-'a',i-1);
for(i=tot-1;i>1;--i) if(f[i]!=0 && f[i]!=1)cnt[f[i]]+=cnt[i];
n=n-((n+1)&1);
for(i=2;i<tot;++i) t[len[i]]+=cnt[i];
for(i=n;i>0;i-=2){
if(t[i]==0) continue;
if(K>=t[i]){K-=t[i];ans=ans*fp((ll)i,t[i])%mod;}
else{ans=ans*fp((ll)i,K)%mod;K=0;}
if(K==0) break;
}
if(K>0) printf("-1\n");else printf("%lld\n",ans);
}