[Luogu5828] 边双连通图计数

之前试图通过用树的生成函数复合边双的生成函数来得到答案...但是问题似乎等价于已知 \(F(x), H(x)\)\(F(G(x)) = H(x)\),求 \([x^n] G(x)\)。我不是很会,如果有人会的话求教我一下 /kel。

下面写的是题解做法。

UPD:树的生成函数其实也可以看做一个拉格朗日反演的结果..所以感性理解一下可以绕过上面那个式子是一个很自然的东西。(纯属胡言乱语)

做法

设有根边双和有根连通图 (有根就是钦定了一个点作根) 的 EGF 分别是 \(F(x)\)\(G(x)\)。(均不包含空图)

众所周知,设无向图的 EGF 为 \(H(x)\),则连通无向图的 EGF 为 \(\ln H(x)\),所以 \(G(x) = \frac {xH'(x)} {H(x)}\) (不用真的按照这个式子去算,就是第 \(i\) 项乘以 \(i\) 的意思)。

考虑根所在的边双恰好为 \(n\) 的连通图数,这样的连通图的生成函数是 \(x^n f_n \sum_k n^k \frac{G^k(x)}{k!}=x^nf_n\exp(nG(x))\)

所以有 \(G(x) = \sum_{n \ge 1} f_n (x\exp G(x))^n=F(x\exp G(x))\)

作 (扩展) 拉格朗日反演可得 \([x^n] F(x) = \frac 1 n[x^{-1}]\frac {G'(x)}{(x\exp G(x))^n}\)

我选择贴板 💊💊💊

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>

using namespace std;

const int maxn = 100010;
const int mod = 998244353;
const int g = 3;

int wa[8*maxn], wb[8*maxn], wc[8*maxn], rev[8*maxn];

inline int mo(int x) {
if (x >= mod) return x - mod;
return x;
}

int qpow(int x, int y) {
int ret = 1;
while (y) {
if (y & 1) ret = 1LL*ret*x%mod;
x = 1LL*x*x%mod;
y >>= 1;
}
return ret;
}

int qpowl(int x, long long y) {
int ret = 1;
while (y) {
if (y & 1) ret = 1LL*ret*x%mod;
x = 1LL*x*x%mod;
y >>= 1;
}
return ret;
}

struct poly {
int *a, len;
poly(int l_ = 0) {
len = l_;
a = new int[l_];
for (int i = 0; i < l_; i++) a[i] = 0;
}
};

void ntt(int *a, int l, int ty) {
rev[0] = 0;
for (int i = 1; i < (1<<l); i++) rev[i] = ((rev[i >> 1] >> 1) | ((i&1) << (l-1)));
for (int i = 0; i < (1<<l); i++) if (rev[i] > i) swap(a[i], a[rev[i]]);
for (int len = 2; len <= (1<<l); len <<= 1) {
int wn = qpow(g, (mod-1)/len);
for (int i = 0; i < (1<<l); i += len) {
int w = 1;
for (int j = 0; j < (len>>1); j++) {
int v1 = a[i+j], v2 = 1LL*w*a[i+j+(len>>1)]%mod;
a[i+j] = mo(v1 + v2);
a[i+j+(len >> 1)] = mo(v1 + mod - v2);
w = 1LL*w*wn%mod;
}
}
}
if (ty == -1) {
int inv = qpow(1<<l, mod-2);
for (int i = 0; i < (1<<l); i++) a[i] = 1LL*a[i]*inv%mod;
for (int i = 1; i < (1<<(l-1)); i++) swap(a[i], a[(1<<l)-i]);
}
}

poly operator*(const poly &p1, const poly &p2) {
poly ret(p1.len + p2.len - 1);
int l = 0; while ((1<<l) < ret.len) ++ l;
for (int i = 0; i < (1<<l); i++) wa[i] = wb[i] = 0;
for (int i = 0; i < p1.len; i++) wa[i] = p1.a[i];
for (int i = 0; i < p2.len; i++) wb[i] = p2.a[i];
ntt(wa, l, 1); ntt(wb, l, 1);
for (int i = 0; i < (1<<l); i++) wc[i] = 1LL*wa[i]*wb[i]%mod;
ntt(wc, l, -1);
for (int i = 0; i < ret.len; i++) ret.a[i] = wc[i];
return ret;
}

poly polyInv(const poly &p) {
if (p.len == 1) {
poly ret(1);
ret.a[0] = qpow(p.a[0], mod-2);
return ret;
}
int nlen = (p.len+1)/2;
poly tmp(nlen); for (int i = 0; i < nlen; i++) tmp.a[i] = p.a[i];
poly G = polyInv(tmp);
poly ret(p.len);
for (int i = 0; i < G.len; i++) ret.a[i] = 1LL*2*G.a[i]%mod;
poly t = p*G*G;
for (int i = 0; i < p.len; i++) ret.a[i] = (ret.a[i] + mod - t.a[i]) % mod;
return ret;
}

poly polyLn(const poly &p) {
if (p.len == 1) return poly(1);
poly inv = polyInv(p);
poly d(p.len);
for (int i = 0; i+1 < p.len; i++) {
d.a[i] = 1LL*p.a[i+1]*(i+1)%mod;
}
inv = inv * d;
poly ret(p.len);
for (int i = 1; i < ret.len; i++) {
ret.a[i] = 1LL*qpow(i, mod-2)*inv.a[i-1]%mod;
}
return ret;
}

poly polyExp(const poly &p) {
if (p.len == 1) {
poly ret(1);
ret.a[0] = 1;
return ret;
}
int nlen = (p.len+1)/2;
poly tmp(nlen); for (int i = 0; i < nlen; i++) tmp.a[i] = p.a[i];
poly G = polyExp(tmp);
poly t(p.len), v(p.len);
for (int i = 0; i < nlen; i++) v.a[i] = G.a[i];
v = polyLn(v);
for (int i = 0; i < p.len; i++) {
t.a[i] = (mod - v.a[i] + p.a[i]) % mod;
}
t.a[0] = (t.a[0] + 1) % mod;
t = t*G;
poly ret(p.len);
for (int i = 0; i < ret.len; i++) ret.a[i] = t.a[i];
return ret;
}

void polyDiv(const poly &F, const poly &Q, poly &P, poly &R) {
poly rF(F.len-Q.len+1), rQ(F.len-Q.len+1);
for (int i = 0; i < rF.len; i++) rF.a[i] = F.a[F.len-1-i];
for (int i = 0; i < rQ.len; i++) if (Q.len-1-i >= 0) rQ.a[i] = Q.a[Q.len-1-i];
poly v = rF*polyInv(rQ);
for (int i = 0; i < P.len; i++) P.a[i] = v.a[F.len-Q.len-i];
poly t = P*Q;
for (int i = 0; i < R.len; i++) R.a[i] = (F.a[i] + mod - t.a[i]) % mod;
}

poly polySqrt(const poly &p) {
if (p.len == 1) {
poly ret(1);
ret.a[0] = 1;
return ret;
}
int nlen = (p.len+1)/2;
poly np(nlen); for (int i = 0; i < nlen; i++) np.a[i] = p.a[i];
poly F0 = polySqrt(np);
poly exF0(p.len);
for (int i = 0; i < F0.len; i++) exF0.a[i] = F0.a[i];
poly tmp = F0*F0;
poly extmp(p.len);
for (int i = 0; i < p.len; i++) {
if (i < tmp.len) extmp.a[i] = tmp.a[i];
extmp.a[i] = (extmp.a[i] + mod - p.a[i]) % mod;
}
poly Q(p.len); for (int i = 0; i < p.len; i++) Q.a[i] = 1LL*exF0.a[i]*2%mod;
poly tt = extmp*polyInv(Q);
poly res(p.len);
for (int i = 0; i < res.len; i++) {
res.a[i] = (exF0.a[i] + mod - tt.a[i]) % mod;
}
return res;
}

int fac[maxn], ifac[maxn];

int main() {
int T = 5;
fac[0] = ifac[0] = 1;
for (int i = 1; i <= 100000; i++) {
fac[i] = 1LL * fac[i-1] * i % mod;
ifac[i] = 1LL * ifac[i-1] * qpow(i, mod-2) % mod;
}
poly G, dG;
{
int n = 100000;
poly H(n + 1);
for (int i = 0; i <= n; i++) {
H.a[i] = 1LL * qpowl(2, 1LL * i * (i - 1) / 2) * ifac[i] % mod;
}
poly lnH = polyLn(H);
G = poly(n + 1);
for (int i = 1; i <= n; i++) {
G.a[i] = 1LL * lnH.a[i] * i % mod;
}

dG = poly(n);
for (int i = 0; i < n; i++) {
dG.a[i] = 1LL * G.a[i+1] * (i+1) % mod;
}
}
while (T --) {
int n;
scanf("%d", &n);
poly nG = poly(n + 1);
for (int i = 0; i <= n; i++) {
nG.a[i] = 1LL * G.a[i] * (mod - n) % mod;
}
poly res = polyExp(nG) * dG;
int ans = 1LL * res.a[n-1] * qpow(n, mod-2) % mod * fac[n - 1] % mod;
printf("%d\n", ans);
}
return 0;
}