题解:P10982 Connected Graph
选自我的多项式与生成函数学习笔记。
- 求
n 个点有标号无向连通图的个数,对1004535809 取模。
记
考虑钦定
将上式变形:
考虑设:
根据变形后的等式,可以发现对于
那么
那么可以计算出
时间复杂度
#include <bits/stdc++.h>
using namespace std; const int N = 1 << 20;
namespace Poly {
using ll = long long; using poly = vector<ll>;
const ll M = 1004535809, G = 3; int R[N];
poly get(int n) { return poly(n + 1); } int dg(poly &a) { return a.size() - 1; }
ll qp(ll x, ll y) {
ll r = 1; for (; y; y >>= 1, x = x * x % M) if (y & 1) r = r * x % M;
return r;
}
void init(int n) {
for (int i = 0; i < n; ++i) {
R[i] = R[i >> 1] >> 1; if (i & 1) R[i] |= n >> 1;
}
}
void NTT(poly &a, int n, bool o = 0) {
if (o) init(n);
for (int i = 0; i < n; ++i) if (i < R[i]) swap(a[i], a[R[i]]);
for (int i = 2; i <= n; i <<= 1) {
ll g = qp(G, (M - 1) / i), w, u, v;
for (int j = 0; j < n; j += i) {
w = 1;
for (int k = j; k < j + (i >> 1); ++k, w = w * g % M)
u = a[k], v = w * a[k + (i >> 1)] % M,
a[k] = (u + v) % M, a[k + (i >> 1)] = (u - v + M) % M;
}
}
}
poly operator+(poly a, poly b) {
int n = max(a.size(), b.size()); a.resize(n); b.resize(n);
poly c = get(n - 1); for (int i = 0; i < n; ++i) c[i] = (a[i] + b[i]) % M;
return c;
}
poly operator-(poly a, poly b) {
int n = max(a.size(), b.size()); a.resize(n); b.resize(n);
poly c = get(n - 1);
for (int i = 0; i < n; ++i) c[i] = (a[i] - b[i] + M) % M; return c;
}
poly operator*(poly a, poly b) {
int n = a.size(), m = b.size(), L = 1; while (L < n + m - 1) L <<= 1;
a.resize(L); b.resize(L); NTT(a, L, 1); NTT(b, L);
for (int i = 0; i < L; ++i) a[i] = a[i] * b[i] % M; NTT(a, L);
poly c = get(n + m - 2); ll inv = qp(L, M - 2); c[0] = a[0] * inv % M;
for (int i = 1; i < n + m - 1; ++i) c[i] = a[L - i] * inv % M; return c;
}
poly mod(poly a, int n) { a.resize(n, 0); return a; }
poly inv(poly a, int n) {
poly c = get(0), d = get(0); c[0] = qp(a[0], M - 2); d[0] = 2;
for (int i = 1; (1 << i - 1) < n; ++i)
c = mod(c * (d - mod(a, 1 << i) * c), 1 << i);
return mod(c, n);
}
}
using namespace Poly; int n; poly f, g, h; ll p[N];
int main() {
cin >> n; f = g = h = get(n); g[0] = 1;
for (ll i = p[0] = 1; i <= n; ++i)
p[i] = p[i - 1] * i % M,
g[i] = qp(2, i * (i - 1) >> 1) * qp(p[i], M - 2) % M,
h[i] = qp(2, i * (i - 1) >> 1) * qp(p[i - 1], M - 2) % M;
f = mod(h * inv(g, n + 1), n + 1); cout << f[n] * p[n - 1] % M; return 0;
}