luoguP1659 [国际集训队]拉拉队排练 manacher算法

时间:2021-08-22 18:39:06

luoguP1659 [国际集训队]拉拉队排练 manacher算法

直接统计长度为$i$的回文子串有多少个

然后倒叙枚举长度,快速幂统计一下即可

复杂度$O(n \log n)$

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; #define ll long long
#define ri register int
#define rep(io, st, ed) for(ri io = st; io <= ed; io ++)
#define drep(io, ed, st) for(ri io = ed; io >= st; io --) const int sid = ;
const int mod = ; inline void inc(int &a, int b) { a += b; if(a >= mod) a -= mod; }
inline void dec(int &a, int b) { a -= b; if(a < ) a += mod; }
inline int mul(int a, int b) { return 1ll * a * b % mod; }
inline int fp(int a, int k) {
int ret = ;
for( ; k; k >>= , a = mul(a, a))
if(k & ) ret = mul(ret , a);
return ret;
} ll n, k;
char s[sid];
int r[sid], num[sid]; int main() {
cin >> n >> k;
scanf("%s", s + );
int mr = , pos = ; r[] = ;
rep(i, , n) {
r[i] = min(mr - i + , r[pos + pos - i]);
while(i - r[i] > && s[i + r[i]] == s[i - r[i]]) r[i] ++;
if(i + r[i] - > mr) mr = i + r[i] - , pos = i;
}
rep(i, , n) num[ * r[i] - ] ++;
drep(i, n, ) num[i] += num[i + ]; int ans = ;
drep(i, n, )
if(i & ) {
if(k >= num[i]) {
ans = mul(ans, fp(i, num[i]));
k -= num[i];
}
else {
ans = mul(ans, fp(i, k));
break;
}
} printf("%d\n", ans);
return ;
}