PE427.n-sequences
题目大意
定义一个串的值为最长相同子串长度,求所有长度为n的每项为[1,n]的所有串的值之和模1e9+9
题解
推完就对了很奇妙
先求至多为m的答案
\(ans_m=[x^n]n\sum_{i=0}^{\infty} (n-1)^i(\sum_{j=1}^m x^j)^{i+1}\)
\(=[x^n]\frac{n}{n-1}\sum_{i=1}^{\infty} (n-1)^i(\sum_{j=1}^m x^j)^i\)
设\(F=(n-1)\sum_{i=1}^m x^i=(n-1)\frac{x^{m+1}-x}{x-1}\)
\(ans_m=[x^n]\frac{n}{n-1}\sum_{i=1}^{\infty} F^i\)
\(=[x^n]\frac{n}{n-1}*\frac{F}{1-F}\)
\(=[x^n]\frac{n}{n-1}*(\frac{1}{1-F}-1)\)
-1没有影响可以去掉
\(=[x^n]\frac{n}{n-1}*\frac{1-x}{1-(nx-(n-1)x^{m+1})}\)
分母展开成无穷级数,枚举\(-(n-1)x^{m+1}\)的次数来计算,\(nx\)的次数就用\(n\)减一下得到
因为实际上是个\((a+b)^i\)的形式,所以当b的次数和总次数确定时只有一项算到
枚举m,时间复杂度是调和级数的形式,预处理幂可以做到O(n log n),跑6s左右
最后的\(Ans=\sum_i i*(ans_i-ans_{i-1})\)
code
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define fo(a,b,c) for (a=b; a<=c; a++)
#define fd(a,b,c) for (a=b; a>=c; a--)
#define C(n,m) (jc[n]*Jc[m]%mod*Jc[(n)-(m)]%mod)
#define add(a,b) a=((a)+(b))%mod
#define mod 1000000009
#define Mod 1000000007
#define ll long long
#define file
using namespace std;
ll jc[7500001],Jc[7500001],Ans[7500001],p1[7500001],p2[7500001],ans;
int n,i,j,k,l;
ll qpower(ll a,int b) {ll ans=1; while (b) {if (b&1) ans=ans*a%mod;a=a*a%mod;b>>=1;} return ans;}
int main()
{
#ifdef file
freopen("PE427.in","r",stdin);
#endif
scanf("%d",&n);
jc[0]=1;
fo(i,1,n) jc[i]=jc[i-1]*i%mod;
Jc[n]=qpower(jc[n],Mod);
fd(i,n-1,0) Jc[i]=Jc[i+1]*(i+1)%mod;
p1[0]=p2[0]=1;
fo(i,1,n) p1[i]=p1[i-1]*(-(n-1))%mod,p2[i]=p2[i-1]*n%mod;
fo(i,1,n)
{
fo(j,0,n)
if ((i+1)*j<=n)
{
add(Ans[i],p1[j]*p2[n-(i+1)*j]%mod*C(j+(n-(i+1)*j),j));
if ((i+1)*j<n)
add(Ans[i],-p1[j]*p2[n-(i+1)*j-1]%mod*C(j+(n-(i+1)*j-1),j));
}
else
break;
}
fo(i,1,n) add(ans,(Ans[i]-Ans[i-1])*i);
printf("%lld\n",(ans+mod)%mod*n%mod*qpower(n-1,Mod)%mod);
fclose(stdin);
fclose(stdout);
return 0;
}