BZOJ3456 城市规划 【多项式求ln】

题目链接

BZOJ3456

题解

真是一道经典好题,至此已经写了分治\(NTT\),多项式求逆,多项式求\(ln\)三种写法

我们发现我们要求的是大小为\(n\)无向联通图的数量
\(n\)个点的无向图是由若干个无向联通图构成的
那么我们设\(F(x)\)为无向联通图数量的指数型生成函数
\(G(x)\)为无向图数量的指数型生成函数

\(G(x)\)很好求

\[G(x) = \frac{F(x)}{1!} + \frac{F^2(x)}{2!} + \frac{F^3(x)}{3!} + \dots = e^{F(x)} \]

\[F(x) = lnG(x) \]

多项式求\(ln\)即可

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b)
#define cls(s) memset(s,0,sizeof(s))
#define cp pair<int,int>
#define LL long long int
using namespace std;
const int maxn = 500005,maxm = 100005,INF = 1000000000;
inline int read(){
	int out = 0,flag = 1; char c = getchar();
	while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
	while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
	return out * flag;
}
const int G = 3,P = 1004535809;
int R[maxn];
inline int qpow(int a,LL b){
	int re = 1;
	for (; b; b >>= 1,a = 1ll * a * a % P)
		if (b & 1) re = 1ll * re * a % P;
	return re;
}
void NTT(int* a,int n,int f){
	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 gn = qpow(G,(P - 1) / (i << 1));
		for (int j = 0; j < n; j += (i << 1)){
			int g = 1,x,y;
			for (int k = 0; k < i; k++,g = 1ll * g * gn % P){
				x = a[j + k],y = 1ll * g * a[j + k + i] % P;
				a[j + k] = (x + y) % P,a[j + k + i] = ((x - y) % P + P) % P;
			}
		}
	}
	if (f == 1) return;
	int nv = qpow(n,P - 2); reverse(a + 1,a + n);
	for (int i = 0; i < n; i++) a[i] = 1ll * a[i] * nv % P;
}
int n,c[maxn],f[maxn],g[maxn],Fv[maxn];
int fac[maxn],fv[maxn],inv[maxn];
void init(){
	fac[0] = fac[1] = fv[0] = fv[1] = inv[0] = inv[1] = 1;
	for (int i = 2; i <= (n << 1); i++){
		fac[i] = 1ll * fac[i - 1] * i % P;
		inv[i] = 1ll * (P - P / i) * inv[P % i] % P;
		fv[i] = 1ll * fv[i - 1] * inv[i] % P;
	}
}
void Inv(int* a,int* b,int deg){
	if (deg == 1){b[0] = qpow(a[0],P - 2); return;}
	Inv(a,b,(deg + 1) >> 1);
	int n = 1,L = 0;
	while (n < (deg << 1)) n <<= 1,L++;
	for (int i = 1; i < n; i++) R[i] = (R[i >> 1] >> 1) | ((i & 1) << (L - 1));
	for (int i = 0; i < deg; i++) c[i] = a[i];
	for (int i = deg; i < n; i++) c[i] = 0;
	NTT(c,n,1); NTT(b,n,1);
	for (int i = 0; i < n; i++)
		b[i] = 1ll * ((2ll - 1ll * c[i] * b[i] % P) + P) % P * b[i] % P;
	NTT(b,n,-1);
	for (int i = deg; i < n; i++) b[i] = 0;
}
void Der(int* a,int& n){
	n--;
	for (int i = 0; i <= n; i++) a[i] = 1ll * a[i + 1] * (i + 1) % P;
}
void Int(int* a,int& n){
	n++;
	for (int i = n; i; i--) a[i] = 1ll * a[i - 1] * inv[i] % P;
}
void Getln(int* a,int* b){
	int deg = n;
	Inv(a,Fv,n + 1);
	Der(a,deg);
	int m = deg + n,n = 1,L = 0;
	while (n <= m) n <<= 1,L++;
	for (int i = 1; i < n; i++) R[i] = (R[i >> 1] >> 1) | ((i & 1) << (L - 1));
	NTT(a,n,1); NTT(Fv,n,1);
	for (int i = 0; i < n; i++) a[i] = 1ll * a[i] * Fv[i] % P;
	NTT(a,n,-1);
	deg = m;
	Int(a,deg);
	for (int i = 0; i <= deg; i++) b[i] = a[i];
}
int main(){
	n = read();
	init();
	f[0] = f[1] = 1;
	for (int i = 2; i <= n; i++)
		f[i] = 1ll * qpow(2,1ll * i * (i - 1) / 2) * fv[i] % P;
	Getln(f,g);
	printf("%lld\n",1ll * g[n] * fac[n] % P);
	return 0;
}

posted @ 2018-06-15 16:34  Mychael  阅读(591)  评论(1编辑  收藏  举报