【国家集训队】拉拉队排练

题面

https://www.luogu.org/problem/P1659

题解

#include<cstdio>
#include<iostream>
#include<cstring>
#define N 1000500
#define ri register int
#define uLL unsigned long long
using namespace std;

char s0[N];
char s1[N<<1];
int n,tonj[N<<1],tono[N<<1];
int hw[N<<1];
uLL k;

inline uLL pow(uLL x,int n) {
  uLL ret=1;
  for (;n;n>>=1,x=x*x%19930726) if (n&1) ret=ret*x%19930726;
  return ret;
}

int main(){
  scanf("%d %llu",&n,&k);
  scanf("%s",s0+1);
  for (ri i=1;i<=2*n+1;i++) {
    if (i%2==1) s1[i]='#'; else s1[i]=s0[i/2];
  }
  int mid=0,maxr=0;
  for (ri i=1;i<=2*n+1;i++) {
    if (i<=maxr) hw[i]=min(hw[2*mid-i],maxr-i);
    while (s1[i+hw[i]+1]==s1[i-hw[i]-1] && i+hw[i]+1<=2*n+1 && i-hw[i]-1>=1) hw[i]++;
    if (i+hw[i]>maxr) maxr=i+hw[i],mid=i;
  }
  for (ri i=1;i<=2*n+1;i++) {
    if (s1[i]=='#') tono[2]++,tono[hw[i]+1]--;
    else tonj[1]++,tonj[hw[i]+1]--;
  }
  for (ri i=1;i<=n;i++) tono[i]+=tono[i-1],tonj[i]+=tonj[i-1];
  uLL tot=0;
  uLL fac=1;
  for (ri i=n;i>=1;i--) {
    if (i%2==1) {
      if (tot+tonj[i]>=k) {
        fac*=pow(i,k-tot);
        fac%=19930726;
        printf("%llu",fac);
        return 0;
      }
      else {
        tot+=tonj[i];
        fac*=pow(i,tonj[i]);
        fac%=19930726;
      }
    }
  }
  puts("-1");
}

 

posted @ 2019-07-31 22:50  HellPix  阅读(186)  评论(0编辑  收藏  举报