zoukankan      html  css  js  c++  java
  • [BZOJ 2160]拉拉队排练

    Description

    题库链接

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

    (1leq Nleq 10^6,1leq 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('
    '))
    #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;
    }
  • 相关阅读:
    和为S的两个数字
    数字在排序数组中出现的次数
    连续子数组的最大和
    包含min函数的栈
    二进制中1的个数
    变态跳台阶
    android里R.layout.的问题
    eclipse里面设置JVM参数的问题
    perl小记
    机器寻径引导算法(最短路径表)__深搜、栈
  • 原文地址:https://www.cnblogs.com/NaVi-Awson/p/8409568.html
Copyright © 2011-2022 走看看