Hdu 5279 YJC plays Minecraft

多项式 $\exp$ .

规定每一块内部不能有环.连接块与块的边随便选.

这样只会多算一种情况,就是块与块的边都被选,并且每块的第一个点和最后一个点都连通.

把这种情况减掉即可,答案为
$$
ans=2^n\prod_{i=1}^n (g_{a_i})-\prod_{i=1}^n (g_{a_i}-h_{a_i})
$$

其中 $g_n,h_n$ 分别表示 $n$ 个点的完全图生成森林的数目, $n$ 个点的完全图, $n$ 与 $n$ 不连通的生成森林数目.

记 $f_n=n^{n-2}$ 表示 $n$ 个点的完全图生成树的数目, $F,G,H$ 分别为三者的 EGF.

易知 $G=\exp F$ ,只需要考虑如何计算 $H$ .

考虑 $h_n$ 如何计算,枚举 $1​$ 所在的生成树大小即可.
$$
h_n=\sum_{i=1}^{n-1}\binom{n-2}{i-1} f_i\cdot g_{n-i} \\
\frac{h_n}{(n-2)!}=\sum_{i=1}^{n-1} \frac{f_i}{(i-1)!}\cdot \frac{g_{n-i}}{(n-i-1)!}
$$
做一次卷积即可求出所有 $h$ ,时间复杂度 $O(n\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
//%std
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
inline int read()
{
int out = 0, fh = 1;
char jp = getchar();
while ((jp > '9' || jp < '0') && jp != '-')
jp = getchar();
if (jp == '-')
fh = -1, jp = getchar();
while (jp >= '0' && jp <= '9')
out = out * 10 + jp - '0', jp = getchar();
return out * fh;
}
void print(int x)
{
if (x >= 10)
print(x / 10);
putchar('0' + x % 10);
}
void write(int x, char c)
{
if (x < 0)
putchar('-'), x = -x;
print(x);
putchar(c);
}
const int P = 998244353, G = 3;
int add(int a, int b)
{
return a + b >= P ? a + b - P : a + b;
}
void inc(int &a, int b)
{
a = add(a, b);
}
int mul(int a, int b)
{
return 1LL * a * b % P;
}
int fpow(int a, ll b)
{
int res = 1;
while (b)
{
if (b & 1LL)
res = mul(res, a);
a = mul(a, a);
b >>= 1;
}
return res;
}
const int N = 1 << 18 | 10;
int rev[N], omega[N], inv[N], curn = 0;
void init(int n)
{
if (curn == n)
return;
for (int i = 0; i < n; ++i)
rev[i] = (rev[i >> 1] >> 1) | ((i & 1) * (n >> 1));
for (int l = 2; l <= n; l <<= 1)
{
omega[l] = fpow(G, (P - 1) / l);
inv[l] = fpow(omega[l], P - 2);
}
curn = n;
}
void DFT(int *a, int n, bool invflag)
{
init(n);
for (int i = 0; i < n; ++i)
if(i < rev[i])
swap(a[i], a[rev[i]]);
for (int l = 2; l <= n; l <<= 1)
{
int m = l >> 1;
int gi = omega[l];
if (invflag)
gi = inv[l];
for (int *p = a; p != a + n; p += l)
{
int g = 1;
for (int i = 0; i < m; ++i)
{
int t = mul(g, p[i + m]);
p[i + m] = add(p[i], P - t);
p[i] = add(p[i], t);
g = mul(g, gi);
}
}
}
if(invflag)
{
int invn = fpow(n, P - 2);
for (int i = 0; i < n; ++i)
a[i] = mul(a[i], invn);
}
}
void NTT(int *A, int *B, int *C, int lenA, int lenB)
{
int lenC = lenA + lenB - 1, n = 1;
while (n < lenC)
n <<= 1;
static int a[N], b[N];
copy(A, A + lenA, a);
fill(a + lenA, a + n, 0);
copy(B, B + lenB, b);
fill(b + lenB, b + n, 0);
DFT(a, n, false);
DFT(b, n, false);
for (int i = 0; i < n; ++i)
C[i] = mul(a[i], b[i]);
DFT(C, n, true);
}
void Inverse(int *A, int *B, int n)
{
int len = 1;
while (len < n)
len <<= 1;
static int res[N], tmp[N];
res[0] = fpow(A[0], P - 2);
for (int i = 2; i <= len; i <<= 1)
{
NTT(A, res, tmp, i, i);
NTT(tmp, res, tmp, i, i);
for (int j = 0; j < i; ++j)
res[j] = add(mul(2, res[j]), P - tmp[j]);
}
copy(res, res + n, B);
}
void Diff(int *A, int n)
{
for (int i = 0; i < n - 1; ++i)
A[i] = mul(i + 1, A[i + 1]);
A[n - 1] = 0;
}
int Inv[N];
void Inte(int *A, int n)
{
for (int i = n + 1; i >= 1; --i)
A[i] = mul(Inv[i], A[i - 1]);
A[0] = 0;
}
void Log(int *A, int *B, int n)
{
static int invA[N], tmp[N];
Inverse(A, invA, n);
copy(A, A + n, tmp);
Diff(tmp, n);
NTT(tmp, invA, tmp, n, n);
Inte(tmp, n);
copy(tmp, tmp + n, B);
}
void Exp(int *A, int *B, int n)
{
int len = 1;
while (len < n)
len <<= 1;
static int res[N], tmp[N];
res[0] = 1;
for (int i = 2; i <= len; i <<= 1)
{
Log(res, tmp, i);
for (int j = 0; j < i; ++j)
tmp[j] = add(A[j], P - tmp[j]);
inc(tmp[0], 1);
NTT(tmp, res, res, i, i);
}
copy(res, res + n, B);
}
int bin[N], fac[N], invfac[N], f[N], g[N], tf[N], tg[N], h[N];
int a[N];
void solve()
{
int n = read(), ans = 0, prod = bin[n];
for (int i = 1; i <= n; ++i)
a[i] = read(), prod = mul(prod, g[a[i]]);
ans = prod;
prod = 1;
for (int i = 1; i <= n; ++i)
prod = mul(prod, add(g[a[i]], P - h[a[i]]));
inc(ans, P - prod);
write(ans, '\n');
}
int main()
{
int n = 100000 + 1;
bin[0] = fac[0] = fac[1] = invfac[0] = invfac[1] = Inv[1] = 1;
bin[1] = 2;
for (int i = 2; i < n; ++i)
{
bin[i] = add(bin[i - 1], bin[i - 1]);
fac[i] = mul(fac[i - 1], i);
Inv[i] = mul(P - P / i, Inv[P % i]);
invfac[i] = mul(invfac[i - 1], Inv[i]);
}
f[0] = 0, f[1] = 1;
for (int i = 2; i < n; ++i)
f[i] = mul(fpow(i, i - 2), invfac[i]);
Exp(f, g, n);
g[0] = 0;
for (int i = 1; i < n; ++i)
{
tf[i] = mul(f[i], i);
tg[i] = mul(g[i], i);
f[i] = mul(f[i], fac[i]);
g[i] = mul(g[i], fac[i]);
}
NTT(tf, tg, h, n, n);
h[0] = h[1] = 0;
for (int i = 2; i < n; ++i)
h[i] = mul(h[i], fac[i - 2]);
int T = read();
while (T--)
solve();
return 0;
}