洛谷5162 - WD与积木

用 $f_i$ 表示 $i$ 个时的高度之和, $g_i$ 表示 $i$ 个时的方案数,显然:

$$
\begin{align}
g_n &= \sum\limits_{i=1}^n {n \choose i} g_{n-i} \\
f_n &= g_n + \sum\limits_{i=1}^n {n \choose i} f_{n-i} \\
ans_n &= f_n \times g_n^{-1}
\end{align}
$$

其实到这里可以直接分治 NTT ,不过我们考虑多项式求逆的做法。
首先把 ${n \choose i}$ 拆开,然后设 $F_n = f_n / n!$ , $G_n = g_n / n!$ , $H_n = 1 / n!$ 则

$$
\begin{align}
G &= (2 - H)^{-1} \\
F &= (G - 1) (2 - H)^{-1} \\
ans_n &= F_n G_n^{-1}
\end{align}
$$

(推导时需要注意常数项系数,$f_0 = 0,g_0 = h_0 = 1$。)

代码:

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
// =================================
// author: memset0
// date: 2019.01.27 22:40:38
// website: https://memset0.cn/
// =================================
#include <bits/stdc++.h>
#define ll long long
namespace ringo {
template <class T> inline void read(T &x) {
x = 0; register char c = getchar(); register bool f = 0;
while (!isdigit(c)) f ^= c == '-', c = getchar();
while (isdigit(c)) x = x * 10 + c - '0', c = getchar();
if (f) x = -x;
}
template <class T> inline void print(T x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) print(x / 10);
putchar('0' + x % 10);
}
template <class T> inline void print(T x, char c) { print(x), putchar(c); }

const int N = 4e5 + 10, mod = 998244353;
int T, n;
int a[N], f[N], g[N], h[N], w[N], rev[N];

int inv(int x) { return !x || x == 1 ? 1 : (ll)(mod - mod / x) * inv(mod % x) % mod; }
inline int fpow(int a, int b) { int s = 1; for (; b; b >>= 1, a = (ll)a * a % mod) if (b & 1) s = (ll)s * a % mod; return s; }

inline void ntt(int *a, int lim) {
for (int i = 0; i < lim; i++) if (i < rev[i]) std::swap(a[i], a[rev[i]]);
for (int len = 1; len < lim; len <<= 1)
for (int i = 0; i < lim; i += (len << 1))
for (int j = 0; j < len; j++) {
int x = a[i + j], y = (ll)w[j + len] * a[i + j + len] % mod;
a[i + j] = (x + y) % mod, a[i + j + len] = (x - y + mod) % mod;
}
}

inline int init(int len) {
int lim = 1, k = 0;
while (lim < len) lim <<= 1, ++k;
for (int i = 0; i < lim; i++) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (k - 1));
for (int len = 1, wn; len < lim; len <<= 1) {
wn = fpow(3, (mod - 1) / (len << 1)), w[len] = 1;
for (int i = 1; i < len; i++) w[i + len] = (ll)w[i + len - 1] * wn % mod;
}
return lim;
}

void polyMul(int *f, int *g, int len) {
static int a[N], b[N];
int lim = init(len << 1), inv_lim = inv(lim);
for (int i = 0; i < lim; i++) a[i] = f[i], b[i] = g[i];
ntt(a, lim), ntt(b, lim);
for (int i = 0; i < lim; i++) a[i] = (ll)a[i] * b[i] % mod;
std::reverse(a + 1, a + lim), ntt(a, lim);
for (int i = 0; i < lim; i++) f[i] = (ll)a[i] * inv_lim % mod;
}

void polyInv(int *f, int *g, int n) {
static int a[N], b[N];
g[0] = inv(f[0]);
for (int len = 2; (len >> 1) < n; len <<= 1) {
int lim = init(len << 1), inv_lim = inv(lim);
for (int i = 0; i < len; i++) a[i] = f[i], b[i] = g[i];
ntt(a, lim), ntt(b, lim);
for (int i = 0; i < lim; i++) a[i] = (ll)a[i] * b[i] % mod * b[i] % mod;
std::reverse(a + 1, a + lim), ntt(a, lim);
for (int i = 0; i < len; i++) g[i] = ((g[i] << 1) - (ll)a[i] * inv_lim % mod + mod) % mod;
}
}

void main() {
read(T);
for (int i = 1; i <= T; i++) read(a[i]), n = std::max(n, a[i] + 1);
h[0] = h[1] = 1;
for (int i = 2; i < n; i++) h[i] = (ll)(mod - mod / i) * h[mod % i] % mod;
for (int i = 2; i < n; i++) h[i] = (ll)h[i] * h[i - 1] % mod;
for (int i = 1; i < n; i++) if (h[i]) h[i] = mod - h[i];
polyInv(h, g, n);
for (int i = 1; i < n; i++) f[i] = g[i];
polyMul(f, g, n);
for (int i = 1; i <= T; i++) print((ll)f[a[i]] * inv(g[a[i]]) % mod, '\n');
}

} signed main() { return ringo::main(), 0; }
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×