有标号二分图计数

\(\text{Problem}:\)有标号二分图计数

\(\text{Solution}:\)

首先考虑有标号二分染色图的个数。设 \(A_{n}\) 表示 \(n\) 个点的有标号二分染色图个数,选择 \(k\) 个白点和 \(n-k\) 个黑点之间任意连边,故有:

\[\begin{aligned} A_{n}&=\sum\limits_{i=0}^{n}\binom{n}{k}2^{k(n-k)}\\ &=n!\cdot 2^{\frac{n^{2}}{2}}\sum\limits_{i=0}^{n}\cfrac{2^{-\frac{k^{2}}{2}}}{k!}\cdot \cfrac{2^{-\frac{(n-k)^{2}}{2}}}{(n-k)!}\\ &=n!\cdot 2^{\frac{n^{2}}{2}}\sum\limits_{i=0}^{n}A_{k}B_{n-k}\\ &=n!\cdot 2^{\frac{n^{2}}{2}}C_{n} \end{aligned} \]

可以利用 \(\text{NTT}\)\(O(n\log n)\) 的时间复杂度内求出一行的 \(A_{n}\)。设其 \(\text{EGF}\)\(f(x)\)

考虑有标号二分图是由若干个有标号连通二分图组成的。设前者的 \(\text{EGF}\)\(F(x)\),后者的 \(\text{EGF}\)\(G(x)\),有:

\[F(x)=\sum\limits_{i=0}^{\infty}\cfrac{G^{i}(x)}{i!}=e^{G(x)} \]

考虑 \(f\)\(G\) 的关系。有 \(k\) 个连通块的二分图染色方案对应有 \(2^{k}\) 种,故有:

\[f(x)=\sum\limits_{i=0}^{\infty}\cfrac{2^{i}G^{i}(x)}{i!}=e^{2G(x)}\\ F(x)^{2}=f(x) \]

利用多项式开根即可在 \(O(n\log n)\) 的时间复杂度内求出 \(F(x)\)\(G(x)\) 也可以求出。

\(\text{Code}:\)

#include <bits/stdc++.h>
#pragma GCC optimize(3)
//#define int long long
#define ri register
#define mk make_pair
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define is insert
#define es erase
#define vi vector<int>
#define vpi vector<pair<int,int>>
using namespace std; const int N=265010, Mod=998244353; 
inline int read()
{
	int s=0, w=1; ri char ch=getchar();
	while(ch<'0'||ch>'9') { if(ch=='-') w=-1; ch=getchar(); }
	while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+(ch^48), ch=getchar();
	return s*w;
}
int n=100001;
vector<int> A,B;
int rev[N],r[24][2],fac[N+5],inv[N+5];
inline int ksc(int x,int p) { int res=1; for(;p;p>>=1, x=1ll*x*x%Mod) if(p&1) res=1ll*res*x%Mod; return res; }
inline void Get_Rev(int T) { for(ri int i=0;i<T;i++) rev[i]=(rev[i>>1]>>1)|((i&1)?(T>>1):0); }
inline void DFT(int T,vector<int> &s,int type)
{
	for(ri int i=0;i<T;i++) if(rev[i]<i) swap(s[i],s[rev[i]]);
	for(ri int i=2,cnt=1;i<=T;i<<=1,cnt++)
	{
		int wn=r[cnt][type];
		for(ri int j=0,mid=(i>>1);j<T;j+=i)
		{
			for(ri int k=0,w=1;k<mid;k++,w=1ll*w*wn%Mod)
			{
				int x=s[j+k], y=1ll*w*s[j+mid+k]%Mod;
				s[j+k]=(x+y)%Mod;
				s[j+mid+k]=x-y;
				if(s[j+mid+k]<0) s[j+mid+k]+=Mod;
			}
		}
	}
	if(!type) for(ri int i=0,inv=ksc(T,Mod-2);i<T;i++) s[i]=1ll*s[i]*inv%Mod;
}
inline void NTT(int n,int m,vector<int> &A,vector<int> B)
{
	int len=n+m;
	int T=1;
	while(T<=len) T<<=1;
	Get_Rev(T);
	A.resize(T), B.resize(T);
	for(ri int i=n+1;i<T;i++) A[i]=0;
	for(ri int i=m+1;i<T;i++) B[i]=0;
	DFT(T,A,1), DFT(T,B,1);
	for(ri int i=0;i<T;i++) A[i]=1ll*A[i]*B[i]%Mod;
	DFT(T,A,0);
}
void GetInv(int n,vector<int> &F,vector<int> G)
{
	if(n==1) { F[0]=ksc(G[0],Mod-2); return; }
	GetInv((n+1)/2,F,G);
	vector<int> A,B;
	int T=1;
	while(T<=n+n) T<<=1;
	Get_Rev(T);
	A.resize(T), B.resize(T);
	for(ri int i=0;i<n;i++) A[i]=F[i], B[i]=G[i];
	DFT(T,A,1), DFT(T,B,1);
	for(ri int i=0;i<T;i++) A[i]=(2ll*A[i]%Mod-1ll*B[i]*A[i]%Mod*A[i]%Mod+Mod)%Mod;
	DFT(T,A,0);
	for(ri int i=0;i<n;i++) F[i]=A[i];
}
struct Node { int x,y; }; int I2;
inline Node operator * (Node a,Node b)
{
	int w1=(1ll*a.x*b.x%Mod+1ll*a.y*b.y%Mod*I2%Mod)%Mod;
	int w2=(1ll*a.x*b.y%Mod+1ll*a.y*b.x%Mod)%Mod;
	return (Node){w1,w2};
}
inline Node KSC(Node x,int p) { Node res=(Node){1,0}; for(;p;p>>=1, x=x*x) if(p&1) res=res*x; return res; }
inline bool Check(int x) { return ksc(x,(Mod-1)/2)==1; }
inline int Rand()
{
	int w=1ll*rand()*rand()%Mod;
	w+=rand()-rand();
	w=(w%Mod+Mod)%Mod;
	return w;
}
inline int Cipolla(int n)
{
	if(!n) return 0;
	int a=0;
	while(Check((1ll*a*a%Mod-n+Mod)%Mod)) a=Rand();
	I2=(1ll*a*a%Mod-n+Mod)%Mod;
	int X1=KSC((Node){a,1},(Mod+1)/2).x;
	return min(X1,Mod-X1);
}
void Getsqrt(int n,vector<int> &F,vector<int> G)
{
	if(n==1) { F[0]=Cipolla(G[0]); return; }
	Getsqrt((n+1)/2,F,G);
	vector<int> A,B;
	A.resize(n), B.resize(n);
	GetInv(n,A,F);
	for(ri int i=0;i<n;i++) B[i]=G[i];
	NTT(n,n,A,B);
	for(ri int i=0,inv2=(Mod+1)/2;i<n;i++) F[i]=1ll*(F[i]+A[i])*inv2%Mod;
}
signed main()
{
	srand(time(NULL));
	r[23][1]=ksc(3,119), r[23][0]=ksc(ksc(3,Mod-2),119);
	for(ri int i=22;~i;i--) r[i][0]=1ll*r[i+1][0]*r[i+1][0]%Mod, r[i][1]=1ll*r[i+1][1]*r[i+1][1]%Mod;
	fac[0]=1;
	for(ri int i=1;i<=N;i++) fac[i]=1ll*fac[i-1]*i%Mod;
	inv[N]=ksc(fac[N],Mod-2);
	for(ri int i=N;i;i--) inv[i-1]=1ll*inv[i]*i%Mod;
	A.resize(n), B.resize(n);
	int g2=Cipolla(2);
	for(ri int i=0;i<n;i++)
	{
		int ww=ksc(g2,1ll*i*i%(Mod-1));
		ww=ksc(ww,Mod-2);
		A[i]=B[i]=1ll*inv[i]*ww%Mod;
	}
	NTT(n,n,A,B);
	for(ri int i=0;i<n;i++) B[i]=1ll*A[i]*ksc(g2,1ll*i*i%(Mod-1))%Mod;
	vector<int> F; F.resize(n);
	Getsqrt(n,F,B);
	for(ri int i=1;i<n;i++) printf("%d\n",1ll*F[i]*fac[i]%Mod);
	return 0;
}
posted @ 2021-04-22 19:03  zkdxl  阅读(102)  评论(0编辑  收藏  举报