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

213人阅读 评论(0)

## Description$Description$

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

## Solution$Solution$

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

fn=2(n2)i=1n1(n1i1)2(ni2)fi

2(n2)(n1)!=i=1nfi(i1)!2(n12)(ni)!

F(x)G(x)H(x)F(x)G(x)=====n02(n2)(n1)!xnn0fn(n1)!xnn02(n2)n!xn(GH)(x)(FH1)(x)

UPD：

G(x)C(x)==n02(n2)n!xnn0fnn!xn

G(x)G(x)==(CG)(x)eC(x)

#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;
}
2
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：29632次
• 积分：2242
• 等级：
• 排名：第19511名
• 原创：181篇
• 转载：0篇
• 译文：0篇
• 评论：0条
友情链接
阅读排行