设f[i]为连通图的数量,g[i]为不连通图的数量,显然有f[i]=2-g[i],g[i]通过枚举1所在连通块大小转移,有g[i]=Σf[j]*C(i-1,j-1)·2,也即f[i]=2-(i-1)!·Σf[j]·2/(j-1)!/(i-j)!。显然是一个卷积形式,可以分治NTT。

  进一步将式子化的更优美一点。设h[i]=2,有f[i]=h[i]-(i-1)!·Σf[j]·h[i-j]/(j-1)!/(i-j)!。阶乘项也可以弄掉,设F[i]=f[i]/(i-1)!,H[i]=h[i]/(i-1)!,G[i]=h[i]/i!,则有F[i]=H[i]-ΣF[j]·G[i-j] (0<j<i),而G[0]=1,不妨设F[0]=H[0]=0,则H[i]=ΣF[j]·G[i-j] (0<=j<=i),标准的卷积。设F(x),G(x),H(x)为其各自的生成函数,则H(x)=F(x)·G(x)。H(x)和G(x)我们很容易就能算出来,所以求一发逆就有F(x)了。

  预定本年度最弱智bug:定义了全局变量inv3预处理3的逆元,在主程序里给其赋值,写了int inv3=……。然后就盯着两份一模一样的代码调了一年。

  upd:原来这个除了阶乘的东西就叫指数型生成函数?

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
#define N 540000
#define P 1004535809
char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<''||c>'')) c=getchar();return c;}
int gcd(int n,int m){return m==?n:gcd(m,n%m);}
int read()
{
int x=,f=;char c=getchar();
while (c<''||c>'') {if (c=='-') f=-;c=getchar();}
while (c>=''&&c<='') x=(x<<)+(x<<)+(c^),c=getchar();
return x*f;
}
int n,f[N],g[N],h[N],fac[N],inv[N],r[N],tmp[N],inv3;
int ksm(int a,int k)
{
int s=;
for (;k;k>>=,a=1ll*a*a%P) if (k&) s=1ll*s*a%P;
return s;
}
void DFT(int *a,int n,int g)
{
for (int i=;i<n;i++) if (i<r[i]) swap(a[i],a[r[i]]);
for (int i=;i<=n;i<<=)
{
int wn=ksm(g,(P-)/i);
for (int j=;j<n;j+=i)
{
int w=;
for (int k=j;k<j+(i>>);k++,w=1ll*w*wn%P)
{
int x=a[k],y=1ll*w*a[k+(i>>)]%P;
a[k]=(x+y)%P,a[k+(i>>)]=(x-y+P)%P;
}
}
}
}
void mul(int *a,int *b,int n,int op)
{
for (int i=;i<n;i++) r[i]=(r[i>>]>>)|(i&)*(n>>);
DFT(a,n,),DFT(b,n,);
if (op==) for (int i=;i<n;i++) a[i]=1ll*a[i]*b[i]%P;
else for (int i=;i<n;i++) a[i]=1ll*a[i]*(P+-1ll*a[i]*b[i]%P)%P;
DFT(a,n,inv3);
int u=ksm(n,P-);
for (int i=;i<n;i++) a[i]=1ll*a[i]*u%P;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("bzoj3456.in","r",stdin);
freopen("bzoj3456.out","w",stdout);
const char LL[]="%I64d\n";
#else
const char LL[]="%lld\n";
#endif
n=read();inv3=ksm(,P-);
fac[]=fac[]=;for (int i=;i<=n;i++) fac[i]=1ll*fac[i-]*i%P;
inv[]=inv[]=;for (int i=;i<=n;i++) inv[i]=P-1ll*(P/i)*inv[P%i]%P;
for (int i=;i<=n;i++) inv[i]=1ll*inv[i-]*inv[i]%P;
for (int i=;i<=n;i++) g[i]=1ll*ksm(,(1ll*i*(i-)>>)%(P-))*inv[i]%P,h[i]=1ll*g[i]*i%P;
int t=;while (t<=(n<<)) t<<=;
f[]=;
for (int i=;i<=t;i<<=)
{
for (int j=;j<i;j++) tmp[j]=g[j];
mul(f,tmp,i<<,);
for (int j=i;j<(i<<);j++) f[j]=;
}
for (int i=n+;i<t;i++) f[i]=;
mul(f,h,t,);
cout<<1ll*f[n]*fac[n-]%P;
return ;
}
05-08 15:32