# 【bzoj2160】【啦啦队排练】manacher（马拉车）+差分+快速幂

[pixiv] https://www.pixiv.net/member_illust.php?mode=medium&illust_id=34562780

Description

Input

Output

Sample Input
5
ababa
Sample Output
45
【样例说明】

HINT

#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;

const ll N=1000000+5;
const ll mod=19930726;

ll n,k,pal[N];
char s[N];
ll a[N],sum[N],ans;

void manacher(){
ll mx=0,id;
for(int i=1;i<=n;i++){
if(i<=mx) pal[i]=min(mx-i+1,pal[2*id-i]);
else pal[i]=1;
while(s[i+pal[i]]==s[i-pal[i]]) pal[i]++;
if(i+pal[i]-1>mx)
mx=i+pal[i]-1,id=i;
}
}
ll pow(ll x,ll aa){
ll rt=1;
while(aa){
if(aa&1) rt=(rt*x)%mod;
x=(x*x)%mod;
aa>>=1;
}
return rt;
}
int main(){
scanf("%I64d%I64d",&n,&k);
scanf("%s",s+1);
s[0]='-',s[n+1]='+';
manacher();
for(int i=1;i<=n;i++)
a[1]++,a[pal[i]*2]--;
for(int i=1;i<=n;i++){
sum[i]=sum[i-1]+a[i];
}
ans=1;
for(int i=n;i>=1;i--){
if(i%2==0) continue;
int p=min(k,sum[i]);
k-=p;
ans=(ans*pow(i,p))%mod;
}
if(k) printf("-1");
else printf("%I64d",ans);
return 0;
}

1、区间处理别总想线段树，数据结构多了又复杂又难写，能简则简

posted @ 2017-10-31 19:09  LinnBlanc  阅读(139)  评论(0编辑  收藏  举报