Link
\(a_i=[\text{i is a border}]\)
\(f_i\)为结束时序列长度为\(i\)的概率,其PGF为\(F(x)\)
\(g_i\)为序列长度达到\(i\)且尚未结束的概率,其OGF为\(G(x)\)
分析得到:

\[\begin{aligned} F(x)+G(x)&=1+xG(x)\\ (\frac xn)^LG(x)&=\sum\limits_{i=1}^La_iF(x)(\frac xn)^{L-i} \end{aligned} \]

推导后得到\(ans=F'(1)=\sum\limits_{i=1}^La_in^i\)

#include<cctype>
#include<cstdio>
const int N=100007,P=10000;
char ibuf[1<<25|1],*iS=ibuf;int pw[N],s[N],next[N];
int read(){int x=0;while(isspace(*iS))++iS;while(isdigit(*iS))(x*=10)+=*iS++&15;return x;}
int main()
{
    fread(ibuf,1,1<<25,stdin);
    int n=read(),t=read();
    for(int i=pw[0]=1;i<=100000;++i) pw[i]=pw[i-1]*n%P;
	while(t--)
    {
	int m=read(),ans=0;
	for(int i=1;i<=m;++i) s[i]=read();
	for(int i=2,k;i<=m;next[i]=s[k+1]==s[i]? ++k:k,++i) for(k=next[i-1];k&&s[k+1]^s[i];k=next[k]);
	for(int i=m;i;i=next[i]) ans+=pw[i]-P,ans+=ans>>31&P;
	printf("%04d\n",ans);
    }
}

相关文章:

  • 2022-01-03
  • 2021-12-07
  • 2021-10-27
  • 2022-03-03
  • 2021-09-23
  • 2021-08-26
  • 2022-01-13
  • 2021-09-12
猜你喜欢
  • 2021-09-15
  • 2021-07-27
  • 2021-12-22
  • 2022-12-23
  • 2021-11-02
  • 2022-01-29
  • 2022-01-19
相关资源
相似解决方案