[BZOJ 2160]拉拉队排练

Description

题库链接

求长度为 $N$ 的字符串中前 $K$ 大的奇数回文串的长度乘积。

$1\leq N\leq 10^6,1\leq K \leq 10^{12}$

Solution

$manacher$ 的板子...统计的时候用桶,从后往前做一次前缀...

Code

//It is made by Awson on 2018.2.3
#include <bits/stdc++.h>
#define LL long long
#define dob complex<double>
#define Abs(a) ((a) < 0 ? (-(a)) : (a))
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define Swap(a, b) ((a) ^= (b), (b) ^= (a), (a) ^= (b))
#define writeln(x) (write(x), putchar('\n'))
#define lowbit(x) ((x)&(-(x)))
using namespace std;
const int N = 1000000;
const int yzh = 19930726;
void read(LL &x) {
    char ch; bool flag = 0;
    for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || 1); ch = getchar());
    for (x = 0; isdigit(ch); x = (x<<1)+(x<<3)+ch-48, ch = getchar());
    x *= 1-2*flag;
}
void print(LL x) {if (x > 9) print(x/10); putchar(x%10+48); }
void write(LL x) {if (x < 0) putchar('-'); print(Abs(x)); }

LL cnt[N+5], n, k, len[N+5];
char ch[N+5];

int quick_pow(LL a, LL b) {
    int ans = 1;
    while (b) {
    if (b&1) ans = 1ll*ans*a%yzh;
    a = a*a%yzh, b >>= 1;
    }
    return ans;
}
void manacher() {
    int mx = 0, po = 0;
    for (int i = 0; i < n; i++) {
    if (mx > i) len[i] = Min(len[(po<<1)-i], mx-i);
    else len[i] = 1;
    while (len[i] <= i && ch[i+len[i]] == ch[i-len[i]]) ++len[i];
    if (len[i]+i > mx) mx = len[i]+i, po = i;
    ++cnt[(len[i]<<1)-1];
    }
}
void work() {
    read(n), read(k); scanf("%s", ch);
    manacher();
    int ans = 1; LL tol = 0;
    for (int i = n; i >= 1 && tol < k; i--)
    if (i&1) {
        if (k >= tol) ans = 1ll*ans*quick_pow(i, Min(cnt[i], k-tol))%yzh;
        tol += cnt[i];
        if (i >= 2) cnt[i-2] += cnt[i];
    }
    if (tol < k) ans = -1;
    writeln(ans);
}
int main() {
    work();
    return 0;
}
posted @ 2018-02-03 15:21  NaVi_Awson  阅读(180)  评论(0编辑  收藏  举报