【洛谷 P1659】 [国家集训队]拉拉队排练(manacher)

题目链接
马拉车+简单膜你

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 11000010;
const int MOD = 19930726;
char b[MAXN], a[MAXN << 1];
int hw[MAXN << 1], ans = 1, n, c[MAXN];
#define ll long long
ll now, m;
int fast_pow(int a, ll k){
    int ans = 1;
    while(k){
       if(k & 1) ans = (ll)ans * a % MOD;
       a = (ll) a * a % MOD;
       k >>= 1;
    }
    return ans;
}
int main(){
    scanf("%d%lld", &n, &m);
    scanf("%s", b);
    a[0] = a[1] = '#';
    for(int i = 0; i < n; ++i)
       a[(i << 1) + 2] = b[i], a[(i << 1) + 3] = '#';
    int maxright = 0, mid; n = (n << 1) + 3;
    for(int i = 1; i < n; ++i){
       if(i < maxright)
         hw[i] = min(hw[(mid << 1) - i], hw[mid] + mid - i);
       else hw[i] = 1;
       while(a[i + hw[i]] == a[i - hw[i]]) ++hw[i];
       if(hw[i] + i > maxright){
         maxright = hw[i] + i;
         mid = i;
       }
       ++c[hw[i] - 1];
    }
    for(int i = (n - 3) >> 1; i; --i){
        if(i & 1 ^ 1) continue;
        now += c[i];
        if(m <= now){ ans = (ll) ans * fast_pow(i, m) % MOD; m = 0; break; }
        m -= now; ans = (ll) ans * fast_pow(i, now) % MOD;
    }
    if(m) ans = -1;
    printf("%d\n", ans);
    return 0;
}

转载于:https://www.cnblogs.com/Qihoo360/p/10848541.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值