[BZOJ 2160] 拉拉队排练 (manacher+差分数组+前缀和)

链接

BZOJ 2160


题意

给出一个长度不超过1e6的字符串,求其中前K(<=1e12)长的奇数回文串长度的乘积,结果对19930726取余。


思路

这是一个回文串计数问题,先跑manacher对每个位置求出最长回文半径,由于只考虑奇数长度回文串,不需要在字符两端加’#’字符。由字符串的单调性,以某个字符为回文中心的最长回文半径R[i]会给[1, R[i]]区间的回文串个数提供+1的增益。之后用差分数组+前缀和便可求出所有长度的回文串个数。之后扫一遍,快速幂得出答案。


代码
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
typedef long long lint;
lint mod;
lint quick_pow(lint a, lint n)
{
    if(!n) return 1 % mod;
    lint tmp = quick_pow(a, n >> 1);
    tmp = tmp * tmp % mod;
    if(n & 1) tmp = tmp * a % mod;
    return tmp;
}
#define maxn (1000010)
char s[maxn];
int R[maxn];
void manacher(char s[], int n, int *R)
{
    int p = 0, mx = 0;
    R[0] = 1;
    for(int i = 1; i < n; i++)
    {
        if(i < mx)
            R[i] = min(R[2*p - i], mx - i);
        else
            R[i] = 1;
        while(s[i + R[i]] == s[i - R[i]])
            R[i]++;
        if(i + R[i] > mx)
            p = i, mx = i + R[i];
    }
}
lint d[maxn], cnt[maxn];
int main()
{
    lint n, K;
    cin >> n >> K;
    scanf("%s", s + 1);
    s[0] = '$';
    manacher(s, n + 1, R);
    for(int i = 1; i <= n; i++)
    {
        d[1]++;
        d[R[i] + 1]--;
    }
    for(int i = 1; i <= n; i++)
    {
        cnt[i] = cnt[i-1] + d[i];
    }
    mod = 19930726;
    lint ans = 1;
    for(int i = n; i; i--) if(cnt[i])
    {
        if(cnt[i] >= K) { ans = ans * quick_pow(i*2 - 1, K) % mod; K = 0; break; }
        else
        {
            ans = ans * quick_pow(i*2 - 1, cnt[i]) % mod;
            K -= cnt[i];
        }
    }
    if(K) cout << -1 << endl;
    else cout << ans << endl;
    return 0;
}

这里写图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值