Description
题库链接( bzoj 权限题,可以去 cogs 交♂ 题库链接2
求含有 \(n\) 个点有标号的简单无向联通图的个数。方案数对 \(1004535809(479\times 2^{21}+1)\) 取模。
\(n\leq 130000\)
Solution
似乎直接算答案比较麻烦。不过似乎一个相似的东西比较容易算,我们记 \(n\) 个点有标号的简单无向图的个数为 \(g(n)\) 。(相较要求的东西而言,少了约束:即不要求联通)。
这个比较好算了,因为简单无向图一共有 \(n\choose 2\) 条边。故答案就是 \(g(n)=2^{n\choose 2}\) 。
考虑 \(g\) 和答案间有什么关系。
不妨记答案为 \(f(n)\) ,我们枚举这个图中的 \(1\) 号点所在的联通块的大小,容易得到:
\[g(n)=\sum_{i=1}^n{n-1\choose i-1}f(i)g(n-i)\]
这个式子一定是成立的,因为它不重不漏地考虑了所有的情况。这是因为 \(1\) 号点所在的联通块大小不同,导致计数一定不重复;并且考虑到了所有情况。
我们试着把他变得好看一点,等号两边同除以 \((n-1)!\) ,那么
\[\frac{g(n)}{(n-1)!}=\sum_{i=1}^n\frac{f(i)}{(i-1)!}\times\frac{g(n-i)}{(n-i)!}\]
观察右边这个式子,我们考虑生成函数,让
\[\begin{aligned}C(x)&=\sum_{i=1}^\infty\frac{g(i)}{(i-1)!}x^i\\F(x)&=\sum_{i=1}^\infty\frac{f(i)}{(i-1)!}x^i\\G(x)&=\sum_{i=0}^\infty\frac{g(i)}{i!}x^i\end{aligned}\]
容易发现
\[\begin{equation}C(x)=F(x)G(x)\end{equation}\]
把 \(g(n)=2^{n\choose 2}\) 带入,其实 \(C(x),G(x)\) 的系数都是已知的,如果解出 \(F(n)\) 就能够得到 \(f(n)\) ,也就是答案。考虑如何解 \(F(x)\) 。
把 \((1)\) 式放在 \(\mod x^{n+1}\) 意义下
\[\begin{aligned}C(x)&\equiv F(x)G(x)&\pmod{x^{n+1}}\\F(x)&\equiv C(x)G^{-1}(x)&\pmod{x^{n+1}}\end{aligned}\]
这样,求出多项式 \(G(x)\) 的逆之后与 \(C(x)\) 做一遍卷积,即可得到 \(F(x)\) 。
取出 \(F(n)\) 乘上 \((n-1)!\) 即可得到 \(f(n)\) ,也就是答案。
Code
#include <bits/stdc++.h>
using namespace std;
const int N = 130000*4;
const int yzh = 1004535809;
int n, C[N+5], G[N+5], invG[N+5], GN[N+5];
int g[N+5], inv[N+5], L, R[N+5], len, tmp[N+5];
int quick_pow(int a, int b) {
int ans = 1;
while (b) {
if (b&1) ans = 1ll*ans*a%yzh;
b >>= 1, a = 1ll*a*a%yzh;
}
return ans;
}
int C2(int n) {return 1ll*n*(n-1)/2%(yzh-1); }
void NTT(int *A, int o) {
for (int i = 0; i < len; i++) if (i < R[i]) swap(A[i], A[R[i]]);
for (int i = 1; i < len; i <<= 1) {
int gn = GN[i], x, y;
if (o == -1) gn = quick_pow(gn, yzh-2);
for (int j = 0; j < len; j += (i<<1)) {
int g = 1;
for (int k = 0; k < i; k++, g = 1ll*g*gn%yzh) {
x = A[j+k], y = 1ll*g*A[j+k+i]%yzh;
A[j+k] = (x+y)%yzh, A[j+k+i] = (x-y+yzh)%yzh;
}
}
}
}
void poly_inv(int *A, int *B, int deg) {
if (deg == 1) {B[0] = quick_pow(A[0], yzh-2); return; }
poly_inv(A, B, (deg+1)>>1);
for (L = 0, len = 1; len <= (deg<<1); len <<= 1) ++L;
for (int i = 0; i < len; i++) R[i] = (R[i>>1]>>1)|((i&1)<<(L-1));
for (int i = 0; i < deg; i++) tmp[i] = A[i];
for (int i = deg; i < len; i++) tmp[i] = 0;
for (int i = (deg+1)>>1; i < len; i++) B[i] = 0;
NTT(tmp, 1), NTT(B, 1);
for (int i = 0; i < len; i++)
B[i] = 1ll*B[i]*(((2ll-1ll*tmp[i]*B[i]%yzh)+yzh)%yzh)%yzh;
NTT(B, -1); int inv = quick_pow(len, yzh-2);
for (int i = 0; i < len; i++) B[i] = 1ll*B[i]*inv%yzh;
}
void work() {
scanf("%d", &n); g[0] = inv[0] = inv[1] = 1;
for (int i = 1; i <= N; i <<= 1) GN[i] = quick_pow(3, (yzh-1)/(i<<1));
for (int i = 2; i <= n; i++) inv[i] = 1ll*(yzh-yzh/i)*inv[yzh%i]%yzh;
for (int i = 1; i <= n; i++) inv[i] = 1ll*inv[i]*inv[i-1]%yzh;
for (int i = 1, lim = 1; i <= n; i++, lim = 1ll*lim*2%yzh)
g[i] = 1ll*g[i-1]*lim%yzh;
for (int i = 0; i <= n; i++) G[i] = 1ll*g[i]*inv[i]%yzh;
for (int i = 1; i <= n; i++) C[i] = 1ll*g[i]*inv[i-1]%yzh;
poly_inv(G, invG, n+1);
for (L = 0, len = 1; len <= (n<<1); len <<= 1) ++L;
for (int i = 0; i < len; i++) R[i] = (R[i>>1]>>1)|((i&1)<<(L-1));
NTT(C, 1), NTT(invG, 1);
for (int i = 0; i < len; i++) C[i] = 1ll*C[i]*invG[i]%yzh;
NTT(C, -1); int inv = quick_pow(len, yzh-2);
for (int i = 0; i < len; i++) C[i] = 1ll*C[i]*inv%yzh;
int ans = C[n];
for (int i = 1; i < n; i++) ans = 1ll*ans*i%yzh;
printf("%d\n", ans);
}
int main() {work(); return 0; }