# [生成函数][NTT][多项式求逆]BZOJ 3456: 城市规划

276人阅读 评论(0)

## Description$Description$$Description$

$n$$n$个有标号点的联通图的方案数。

## Solution$Solution$$Solution$

${f}_{n}$$f_n$$n$$n$个有标号点的联通图的方案数。

${f}_{n}={2}^{\left(\genfrac{}{}{0}{}{n}{2}\right)}-\sum _{i=1}^{n-1}\left(\genfrac{}{}{0}{}{n-1}{i-1}\right){2}^{\left(\genfrac{}{}{0}{}{n-i}{2}\right)}{f}_{i}$

$\frac{{2}^{\left(\genfrac{}{}{0}{}{n}{2}\right)}}{\left(n-1\right)!}=\sum _{i=1}^{n}\frac{{f}_{i}}{\left(i-1\right)!}\frac{{2}^{\left(\genfrac{}{}{0}{}{n-i}{2}\right)}}{\left(n-i\right)!}$

$\begin{array}{}\text{(11)}& F\left(x\right)& =& \sum _{n\ge 0}\frac{{2}^{\left(\genfrac{}{}{0}{}{n}{2}\right)}}{\left(n-1\right)!}{x}^{n}\text{(12)}& G\left(x\right)& =& \sum _{n\ge 0}\frac{{f}_{n}}{\left(n-1\right)!}{x}^{n}\text{(13)}& H\left(x\right)& =& \sum _{n\ge 0}\frac{{2}^{\left(\genfrac{}{}{0}{}{n}{2}\right)}}{n!}{x}^{n}\text{(14)}& F\left(x\right)& =& \left(G\ast H\right)\left(x\right)\text{(15)}& G\left(x\right)& =& \left(F\ast {H}^{-1}\right)\left(x\right)\end{array}$

UPD：

$\begin{array}{}\text{(16)}& G\left(x\right)& =& \sum _{n\ge 0}\frac{{2}^{\left(\genfrac{}{}{0}{}{n}{2}\right)}}{n!}{x}^{n}\text{(17)}& C\left(x\right)& =& \sum _{n\ge 0}\frac{{f}_{n}}{n!}{x}^{n}\end{array}$

$\begin{array}{}\text{(18)}& {G}^{\prime }\left(x\right)& =& \left({C}^{\prime }\ast G\right)\left(x\right)\text{(19)}& G\left(x\right)& =& {e}^{C}\left(x\right)\text{(20)}& C\left(x\right)& =& \mathrm{ln}G\left(x\right)\end{array}$

#include <bits/stdc++.h>
using namespace std;

const int N = 404040;
const int MOD = 1004535809;
const int G = 3;
typedef long long ll;

inline char get(void) {
static char buf[100000], *S = buf, *T = buf;
if (S == T) {
T = (S = buf) + fread(buf, 1, 100000, stdin);
if (S == T) return EOF;
}
return *S++;
}
static char c; x = 0;
for (c = get(); c < '0' || c > '9'; c = get());
for (; c >= '0' && c <= '9'; c = get()) x = x * 10 + c - '0';
}

int w[2][N];
int g, ig, num;
int n, m;
int R[N];
int fac[N], inv[N];
int F[N], H[N], iH[N];

inline int Pow(int a, int b) {
int c = 1;
while (b) {
if (b & 1) c = (ll)c * a % MOD;
b >>= 1; a = (ll)a * a % MOD;
}
return c;
}
inline int Inv(int x) {
return Pow(x, MOD - 2);
}
inline int Mod(int x) {
while (x >= MOD) x -= MOD; return x;
}
void Prep(int n) {
g = Pow(G, (MOD - 1) / n);
ig = Inv(g); num = n;
w[0][0] = w[1][0] = 1;
for (int i = 1; i <= n; i++) {
w[0][i] = (ll)w[0][i - 1] * ig % MOD;
w[1][i] = (ll)w[1][i - 1] * g % MOD;
}
}
inline void FFT(int *a, int n, int r) {
static int x, y, INV;
for (int i = 0; i < n; i++)
if (R[i] > i) swap(a[i], a[R[i]]);
for (int i = 1; i < n; i <<= 1)
for (int j = 0; j < n; j += (i << 1))
for (int k = 0; k < i; k++) {
x = a[j + k]; y = (ll)a[j + k + i] * w[r][num / (i << 1) * k] % MOD;
a[j + k] = Mod(x + y); a[j + k + i] = Mod(x - y + MOD);
}
if (!r) {
INV = Inv(n);
for (int i = 0; i < n; i++)
a[i] = (ll)a[i] * INV % MOD;
}
}
void GetInv(int *a, int *b, int n) {
static int tmp[N];
if (n == 1) return (void)(b[0] = Inv(a[0]));
GetInv(a, b, n >> 1);
for (int i = 0; i < n; i++) {
tmp[i] = a[i]; tmp[i + n] = 0;
}
int L = 0; while (!(n >> L & 1)) L++;
for (int i = 1; i < (n << 1); i++)
R[i] = (R[i >> 1] >> 1) | ((i & 1) << L);
FFT(tmp, n << 1, 1); FFT(b, n << 1, 1);
for (int i = 0; i < (n << 1); i++)
tmp[i] = (ll)b[i] * (2 + MOD - (ll)tmp[i] * b[i] % MOD) % MOD;
FFT(tmp, n << 1, 0);
for (int i = 0; i < n; i++) {
b[i] = tmp[i]; b[n + i] = 0;
}
}
inline int Calc(int x) {
return (ll)x * (x - 1) / 2 % (MOD - 1);
}

int main(void) {
freopen("1.in", "r", stdin);
for (m = 1; m <= n; m <<= 1);
for (int i = 2; i < m; i++)
inv[i] = (ll)(MOD - MOD / i) * inv[MOD % i] % MOD;
fac[0] = inv[0] = 1;
for (int i = 1; i < m; i++) {
fac[i] = (ll)fac[i - 1] * i % MOD;
inv[i] = (ll)inv[i - 1] * inv[i] % MOD;
}
for (int i = 0; i < m; i++) {
F[i] = (ll)Pow(2, Calc(i)) * inv[i - 1] % MOD;
H[i] = (ll)Pow(2, Calc(i)) * inv[i] % MOD;
}
Prep(m << 1); GetInv(H, iH, m);
FFT(F, m <<= 1, 1); FFT(iH, m, 1);
for (int i = 0; i < m; i++) F[i] = (ll)F[i] * iH[i] % MOD;
FFT(F, m, 0);
printf("%d\n", (ll)F[n] * fac[n - 1] % MOD);
return 0;
}

个人资料
等级：
访问量： 4万+
积分： 2936
排名： 1万+
友情链接
最新评论