# BZOJ2160:拉拉队排练(Manacher)

5 3
ababa

45
【样例说明】

## Code

 1 #include<iostream>
2 #include<cstring>
3 #include<cstdio>
4 #include<algorithm>
5 #define N (2000000+1000)
6 #define MOD (19930726)
7 using namespace std;
8
9 long long n,k,tot,len[N],sum[N];
10 char a[N],s[N];
11
12 long long Qpow(long long a,long long b,long long p)
13 {
14     long long ans=1,base=a;
15     while (b!=0)
16     {
17         if (b&1) ans=ans*base%p;
18         base=base*base%p; b>>=1;
19     }
20     return ans;
21 }
22
23 void Manacher()
24 {
25     long long x,mid=0,maxn=0;
26     for (int i=1; i<=tot; ++i)
27     {
28         if (i>maxn) x=1;
29         else x=min(maxn-i+1,len[mid*2-i]);
30         while (s[i+x]==s[i-x]) x++;
31         len[i]=x;
32         if (i+x-1>maxn) maxn=i+x-1,mid=i;
33     }
34 }
35
36 int main()
37 {
38     scanf("%lld%lld%s",&n,&k,a);
39
40     s[++tot]='@'; s[++tot]='#';
41     for (int i=0; i<n; ++i)
42         s[++tot]=a[i], s[++tot]='#';
43     s[++tot]='\$';
44     Manacher();
45
46     for (int i=3; i<tot-1; i+=2)
47         sum[1]++,sum[len[i]]--;
48     for (int i=1; i<=tot; ++i)
49         sum[i]+=sum[i-1];
50
51     long long ans=1;
52     for (int i=n%2?n:n-1; i>=1; i-=2)
53         if (sum[i]<=k) (ans*=Qpow(i,sum[i],MOD))%=MOD,k-=sum[i];
54         else {(ans*=Qpow(i,k,MOD))%=MOD; break;}
55     printf("%lld",ans);
56 }
posted @ 2018-08-09 08:09  Refun  阅读(145)  评论(0编辑  收藏  举报