Luogu

分析

设 $f_i$ 为 $i$ 个点的无向连通图个数,$g_i$ 为 $i$ 个点的无向图个数,那么显然有
$$
g_i=2^{i\choose 2}
$$
枚举 $1$ 所在联通块大小可以得到
$$
g_n=\sum_{i=1}^n{n-1\choose i-1}f_ig_{n-i}
$$
把组合数拆开再变形一下得到
$$
\frac{g_n}{(n-1)!}=\sum_{i=1}^n\frac{f_i}{(i-1)!}\times\frac{g_{n-i}}{(n-i)!}
$$

$$
\frac{g_{n+1}}{n!}=\sum_{i=0}^{n}\frac{f_{i+1}}{i!}\times\frac{g_{n-i}}{(n-i)!}
$$
构造 EGF
$$
\begin{aligned}
G(x)&=\sum_{i=0}^{+\infty}\frac{g_{i+1}}{i!}\\
F(x)&=\sum_{i=0}^{+\infty}\frac{f_{i+1}}{i!}\\
H(x)&=\sum_{i=0}^{+\infty}\frac{g_i}{i!}
\end{aligned}
$$
那么有
$$
G(x)=F(x)H(x)
$$

$$
F(x)=\frac{G(x)}{H(x)}
$$
多项式求逆后卷起来求出 $[x^{n-1}]F(x)$ 从而求出 $f_n$ 即可。

代码

// ====================================
//   author: M_sea
//   website: https://m-sea-blog.com/
// ====================================
#include <bits/stdc++.h>
#define file(x) freopen(#x".in","r",stdin); freopen(#x".out","w",stdout)
using namespace std;
typedef long long ll;

int read() {
    int X=0,w=1; char c=getchar();
    while (c<'0'||c>'9') { if (c=='-') w=-1; c=getchar(); }
    while (c>='0'&&c<='9') X=X*10+c-'0',c=getchar();
    return X*w;
}

const int N=524288+10;
const int mod=1004535809;
int qpow(int a,int b) { int c=1;
    for (;b;b>>=1,a=1ll*a*a%mod) if (b&1) c=1ll*c*a%mod;
    return c;
}

int r[N];
void NTT(int* A,int n,int op) {
    for (int i=0;i<n;++i) if (i<r[i]) swap(A[i],A[r[i]]);
    for (int i=1;i<n;i<<=1) {
        int rot=qpow(op==1?3:334845270,(mod-1)/(i<<1));
        for (int j=0;j<n;j+=i<<1)
            for (int k=0,w=1;k<i;++k,w=1ll*w*rot%mod) {
                int x=A[j+k],y=1ll*w*A[j+k+i]%mod;
                A[j+k]=(x+y)%mod,A[j+k+i]=(x-y+mod)%mod;
            }
    }
    if (op==-1) { int inv=qpow(n,mod-2);
        for (int i=0;i<n;++i) A[i]=1ll*A[i]*inv%mod;
    }
}

void Getinv(int* F,int* G,int n) {
    static int A[N],B[N];
    if (n==1) { G[0]=qpow(F[0],mod-2); return; }
    Getinv(F,G,n>>1);
    for (int i=0;i<n;++i) A[i]=F[i],B[i]=G[i];
    int lim=1,l=-1;
    for (;lim<=n;lim<<=1,++l);
    for (int i=0;i<lim;++i) r[i]=(r[i>>1]>>1)|((i&1)<<l);
    NTT(A,lim,1),NTT(B,lim,1);
    for (int i=0;i<lim;++i) A[i]=1ll*A[i]*B[i]%mod*B[i]%mod;
    NTT(A,lim,-1);
    for (int i=0;i<n;++i) G[i]=(2ll*G[i]-A[i]+mod)%mod;
    for (int i=0;i<lim;++i) A[i]=B[i]=0;
}

int n;
int fac[N],ifac[N];
int F[N],G[N],H[N],iH[N],g[N];

int main() {
    n=read();
    fac[0]=1;
    for (int i=1;i<=n;++i) fac[i]=1ll*fac[i-1]*i%mod;
    ifac[n]=qpow(fac[n],mod-2);
    for (int i=n;i;--i) ifac[i-1]=1ll*ifac[i]*i%mod;
    g[0]=g[1]=1;
    for (int i=2;i<=n;++i) g[i]=qpow(2,1ll*i*(i-1)/2%(mod-1));
    for (int i=0;i<=n;++i) G[i]=1ll*g[i+1]*ifac[i]%mod;
    for (int i=0;i<=n;++i) H[i]=1ll*g[i]*ifac[i]%mod;
    int lim=1,l=-1;
    for (;lim<=n+n;lim<<=1,++l);
    Getinv(H,iH,lim);
    for (int i=0;i<lim;++i) r[i]=(r[i>>1]>>1)|((i&1)<<l);
    NTT(G,lim,1),NTT(iH,lim,1);
    for (int i=0;i<lim;++i) F[i]=1ll*G[i]*iH[i]%mod;
    NTT(F,lim,-1);
    printf("%lld\n",1ll*F[n-1]*fac[n-1]%mod);
    return 0;
}
最后修改:2020 年 08 月 13 日 08 : 07 PM