[LOJ6363] 「地底蔷薇」

做法

设有根无向连通图的 EGF 为 \(A(x) = \sum_k a_k\frac{x^k}{k!}\),点双连通图的 EGF 为 \(B(x) = \sum_{k} b_k \frac{x^k}{k!}\) (均不含空图,一个点不算点双连通图)

\[ A(x) = x\sum_{k \ge 0} \frac{(\sum_{s \ge 2} A(x)^{s-1}\frac {b_s} {(s-1)!})^k}{k!} = x \exp(B'(A(x))) \\\\ B'(A(x)) = \ln(\frac {A(x)} x) \]

用拉格朗日反演求 \([x^n]B(x)\) 的时间复杂度为 \(\mathcal O(n \log n)\),于是我们可以对每个 \(i \in S\)\(\mathcal O(i \log i)\) 的时间复杂度计算 \([x^i]B(i)\),然后仅保留这些项得到一个题目允许的点双连通分量的生成函数 \(C(x)\)

我们可以得到所有点双连通分量大小都在 \(S\) 内的有根无向连通图的生成函数 \(D(x)\) (下面用 \(D(x)^{-1}\) 表示 \(D(x)\) 的复合逆) :

\[ C'(D(x)) = \ln(\frac {D(x)} x) \\\\ C'(x) = \ln (\frac {x}{D(x)^{-1}}) \\\\ D(x)^{-1} = \frac x {\exp C'(x)} \]

所以 \(D(x)\)\(\frac x {\exp C'(x)}\) 的复合逆,作拉格朗日反演即可。

时间复杂度 \(\mathcal O((n + \sum_{x \in S} x) \log 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>

using namespace std;

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

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

int mo(int x) {
if (x >= mod) {
return x - mod;
} else {
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 qpow(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;
}
};

int w[maxn * 8];
unsigned long long _a[maxn * 8];

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++) _a[rev[i]] = a[i];
int _ = 0;
for (int len = 2; len <= (1<<l); len <<= 1) {
int _wn = wn[++ _];
w[0] = 1;
for (int i = 1; i < len; i++) {
w[i] = 1LL * w[i-1] * _wn % mod;
}
for (int i = 0; i < (1<<l); i += len) {
for (int j = 0; j < (len>>1); j++) {
unsigned long long v1 = _a[i+j], v2 = _a[i+j+(len>>1)]*w[j]%mod;
_a[i+j] = v1 + v2;
_a[i+j+(len >> 1)] = v1 + mod - v2;
}
}
if (len == (1 << 15)) {
for (int j = 0; j < (1<<l); j++) {
_a[j] = _a[j] % mod;
}
}
}
for (int i = 0; i < (1<<l); i++) {
a[i] = _a[i] % 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;
}

poly A, lnA, dlnA;
int ok[maxn], fac[maxn], ifac[maxn];

void init(int n) {
fac[0] = ifac[0] = 1;
for (int i = 1; i <= n; i++) {
fac[i] = 1LL * fac[i-1] * i % mod;
ifac[i] = qpow(fac[i], mod-2);
}
poly tmp(n + 2);
for (int i = 0; i <= n + 1; i++) {
tmp.a[i] = 1LL * ifac[i] * qpow(2, 1LL * i * (i - 1) / 2) % mod;
}
poly lnt = polyLn(tmp);
A = poly(n + 1);
for (int i = 0; i <= n; i++) {
A.a[i] = 1LL * (i + 1) * lnt.a[i + 1] % mod;
}
lnA = polyLn(A);
dlnA = poly(n);
for (int i = 0; i < n; i++) {
dlnA.a[i] = 1LL * (i + 1) * lnA.a[i + 1] % mod;
}
}

int cal(int n) {
poly nA(n + 1);
for (int i = 0; i <= n; i++) {
nA.a[i] = 1LL * lnA.a[i] * (mod - n) % mod;
}
poly T = polyExp(nA) * dlnA;
return 1LL * qpow(n, mod-2) % mod * T.a[n - 1] % mod;
}

int main() {
int n, m; scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) {
int x; scanf("%d", &x);
ok[x] = 1;
}
for (int i = 0; i <= 20; i++) wn[i] = qpow(g, (mod - 1) / (1 << i));
init(n);
poly dC(n + 1);
for (int i = 0; i <= n; i++) {
if (ok[i + 1]) {
dC.a[i] = cal(i);
}
}
for (int i = 0; i <= n; i++) {
dC.a[i] = 1LL * dC.a[i] * n % mod;
}
poly t = polyExp(dC);
int ans = 1LL * t.a[n - 1] * fac[n - 1] % mod * qpow(n, mod - 2) % mod;
printf("%d\n", ans);
return 0;
}