Bzoj2160: 拉拉队排练

题面

传送门

Sol

\(Manacher\),开桶记录相同半径的有多少
后缀和后乘法原理

# include <bits/stdc++.h>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(2e6 + 5);
const int Zsy(19930726);

IL ll Input(){
	RG ll x = 0, z = 1; RG char c = getchar();
	for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
	for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
	return x * z;
}

int n, p[_], ans = 1;
ll k, cnt[_];
char s[_];

IL int Pow(RG ll x, RG ll y){
	RG ll ret = 1; x %= Zsy;
	for(; y; y >>= 1, x = x * x % Zsy)
		if(y & 1) ret = ret * x % Zsy;
	return ret;
}

int main(RG int argc, RG char *argv[]){
	n = Input(), k = Input(), scanf(" %s", s + 1);
	for(RG int i = 1, id = 0, mx = 0; i <= n; i++){
		if(i < mx) p[i] = min(mx - i, p[2 * id - i]);
		while(i - p[i] && i + p[i] <= n && s[i - p[i]] == s[i + p[i]]) p[i]++;
		if(i + p[i] > mx) mx = i + p[i], id = i;
	}
	for(RG int i = 1; i <= n; ++i) ++cnt[p[i]];
	for(RG int i = n - 1; i; --i) cnt[i] += cnt[i + 1];
	for(RG int i = n; i; --i){
		if(k >= cnt[i]) k -= cnt[i], ans = 1LL * ans * Pow((i << 1) - 1, cnt[i]) % Zsy;
		else{
			ans = 1LL * ans * Pow((i << 1) - 1, k) % Zsy, k = 0;
			break;
		}
	}
	k ? puts("-1") : printf("%d\n", ans);
	return 0;
}

posted @ 2018-03-16 22:38  Cyhlnj  阅读(172)  评论(0编辑  收藏  举报