# [BZOJ2160]拉拉队排练（回文树）

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define erp(i,a,b) for(int i=a;i>=b;--i)
#define LL long long
using namespace std;
const int mo = 19930726;
const int MAXN = 1000005;
int N;
LL sum[MAXN], K;

int ksm(int a, int b)
{
int res = 1;
for (; b; b>>=1, a=1ll*a*a%mo)
if(b&1) res = 1ll*res*a%mo;
return res;
}

int ch[MAXN][26], fa[MAXN];
int cnt[MAXN], len[MAXN];
int last, n, ncnt;
char s[MAXN];
inline int NewNode(int l)
{
return len[ncnt] = l, ncnt++;
}
void initpam()
{
NewNode(0), NewNode(-1);
s[0] = -1, fa[0] = 1;
}
inline int getfail(int x)
{
while (s[n-len[x]-1] != s[n]) x = fa[x];
return x;
}

void ins(int c)
{
s[++n] = c;
int cur = getfail(last);
if (!ch[cur][c])
{
int now = NewNode(len[cur]+2);
fa[now] = ch[getfail(fa[cur])][c];
ch[cur][c] = now;
}
last = ch[cur][c];
cnt[last] ++;
}

void calc()
{
erp(i, ncnt-1, 0) cnt[fa[i]] += cnt[i];
for (int i = 0; i<ncnt; ++i)
if (len[i]>0&&(len[i]&1)) sum[len[i]] += cnt[i];
}

int main()
{
freopen("rehearse.in","r",stdin);
freopen("rehearse.out","w",stdout);
char c;
scanf("%d %lld\n", &N, &K);
initpam();
while (1)
{
c = getchar();
if (c>='a'&&c<='z') ins(c-'a');
else break;
}
calc();
int ans = 1;
for (int i = N; i; --i)
{
if (!(i&1)) continue;
if (K<=sum[i]) { ans = 1ll*ans*ksm(i, K)%mo; K = 0; break; }
else ans = 1ll*ans*ksm(i, sum[i])%mo, K -= sum[i];
}
if (K) puts("-1");
else printf("%d\n", ans);
return 0;
}


• 本文已收录于以下专栏：

举报原因： 您举报文章：[BZOJ2160]拉拉队排练（回文树） 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)