Loj 3315 抽卡

除了老生常谈的拉格朗日反演之外,还有一个 dp + 分治 NTT 的做法.

问题转化

每经过一个非终止状态,步数都会加一,答案可以转化为每个非终止状态期望经过的次数之和.

考虑一个有 $i$ 张牌的非终止状态,会经过它的概率是 ${\binom m i}^{-1}$ ,到达它之后期望停留的步数为 $\frac{m}{m-i}$ .

如果我们能求出 $cnt(i)$ ,表示有 $i$ 张牌的非终止状态(即不存在 k-顺子 的状态)的数目,那么答案就是
$$
ans=\sum_{i=0}^{m-1} \binom m i^{-1}\frac m {m-i}\cdot cnt(i)
$$

dp 设计

每个值域连续段可以分开计算,最后卷在一起.于是只需要求出牌为 $1,2,3,\dots,m$ 时,有 $i$ 张牌的非终止状态数目.

设 $dp(i,j)$ 表示考虑了前 $i$ 张牌,选了 $j$ 张牌,且没有出现 k-顺子的方案数.

讨论第 $i$ 张牌选不选,再减去选了 $i$ 出现的不合法的情况,即 $i-k+1,\dots i$ 这些牌形成了 k-顺子,而 $i-k$ 没有被选.
$$
dp(i,j)=dp(i-1,j-1)+dp(i-1,j)-dp(i-k-1,j-k)
$$
我们需要求出每个 $dp(m,j)$ ,按照转移式朴素地进行 dp 是 $O(m^2)$ 的.

分治 NTT 优化

考虑将每个 $dp(i)$ 用一个多项式 $F_i$ 表示,即,设 $F_i=\sum_{j=0}^i dp(i,j)\cdot x^j$ ,那么转移式可以表示成
$$
F_i=(x+1)F_{i-1}-x^k\cdot F_{i-k-1}
$$
可以看成是上楼梯,每次上 $1$ 级有 $x+1$ 种方案,每次上 $k+1$ 级有 $-x^k$ 种方案,枚举上 $k+1$ 级的次数,可得
$$
F_m=\sum_{i=0}^{\lfloor m /(k+1)\rfloor}(-x^k)^{i}\cdot (x+1)^{m-i(k+1)}\cdot \binom{m-ik}{i}
$$
考虑利用分治 NTT 优化,令 $n=\lfloor m /(k+1)\rfloor$ ,
$$
\begin{aligned}
F_m&=\sum_{i=0}^{\lfloor m /(k+1)\rfloor}(-x^k)^{i}\cdot (x+1)^{m-i(k+1)}\cdot \binom{m-ik}{i}\\
&=(x+1)^{m\bmod (k+1)}\sum_{i=0}^{n}(-x^k)^i\cdot [(x+1)^{(k+1)}]^{(n-i)}\cdot\binom{m-ik}{i}
\end{aligned}
$$
令 $A=-x^k,B=(x+1)^{k+1},coef_i=\binom{m-ik}{i}​$ ,则后面那个式子就是 $\sum_{i=0}^n A^i\cdot B^{n-i}\cdot coef_i​$ .

设 $G(l,r)=\sum_{i=l}^r A^{i-l} B^{r-i}coef_i$ ,则 $G(l,r)=G(l,mid)\cdot B^{r-mid}+G(mid+1,r)\cdot A^{mid+1-l}$ .

时间复杂度 $O(m\log^2 m)​$ .

注意当 $i=k$ 时也有 $-x^k\cdot F_{i-k-1}$ 的转移,答案应该是 $G(0,m) + [m\ge k]x^k\cdot G(0,m-k)$ .

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
//%std
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define y1 ysgh
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, int b)
{
int res = 1;
while (b)
{
if (b & 1)
res = mul(res, a);
a = mul(a, a);
b >>= 1;
}
return res;
}
const int N = 1 << 19 | 10;
int curn = 0, invn, omega[N], inv[N], rev[N];
void init(int n)
{
if (n == curn)
return;
curn = n;
invn = fpow(n, P - 2);
for (int l = 2; l <= n; l <<= 1)
{
omega[l] = fpow(G, (P - 1) / l);
inv[l] = fpow(omega[l], P - 2);
}
for (int i = 0; i < n; ++i)
rev[i] = (rev[i >> 1] >> 1) | ((i & 1) * (n >> 1));
}
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 gi = omega[l];
if (invflag)
gi = inv[l];
int m = l >> 1;
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)
{
for (int i = 0; i < n; ++i)
a[i] = mul(a[i], invn);
}
}
typedef vector<int> poly;
poly operator + (const poly &A, const poly &B)
{
int len = max(A.size(), B.size());
poly C(len);
if (A.size() < B.size())
{
for (int i = 0; i < A.size(); ++i)
C[i] = add(A[i], B[i]);
for (int i = A.size(); i < B.size(); ++i)
C[i] = B[i];
}
else
{
for (int i = 0; i < B.size(); ++i)
C[i] = add(B[i], A[i]);
for (int i = B.size(); i < A.size(); ++i)
C[i] = A[i];
}
return C;
}
poly operator * (const poly &A, const poly &B)
{
static int a[N], b[N], c[N];
int len = A.size() + B.size() - 1, n = 1;
poly C(len);
while (n < len)
n <<= 1;
int lenA = A.size(), lenB = B.size();
for (int i = 0; i < lenA; ++i)
a[i] = A[i];
for (int i = lenA; i < n; ++i)
a[i] = 0;
for (int i = 0; i < lenB; ++i)
b[i] = B[i];
for (int i = lenB; i < n; ++i)
b[i] = 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);
for (int i = 0; i < len; ++i)
C[i] = c[i];
return C;
}
void debug(poly A)
{
for (int i = 0; i < A.size(); ++i)
write(A[i], ' ');
puts("");
}
void shift(poly &A, int p)
{
int len = A.size();
A.resize(len + p);
for (int i = len + p - 1; i >= p; --i)
A[i] = A[i - p];
for (int i = p - 1; i >= 0; --i)
A[i] = 0;
}
int k, fac[N], invfac[N];
int binom(int x, int y)
{
if (x < 0 || y < 0 || x < y)
return 0;
return mul(fac[x], mul(invfac[y], invfac[x - y]));
}
poly binom_poly(int n)
{
poly A(n + 1);
for (int i = 0; i <= n; ++i)
A[i] = binom(n, i);
return A;
}
poly Prod(int l, int r, int m)
{
if (l == r)
return poly{binom(m - l * k, l)};
int mid = (l + r) >> 1;
poly L = Prod(l, mid, m) * binom_poly((r - mid) * (k + 1));
poly R = Prod(mid + 1, r, m);
shift(R, (mid + 1 - l) * k);
if ((mid + 1 - l) & 1)
for (int i = 0; i < R.size(); ++i)
R[i] = add(0, P - R[i]);
return L + R;
}
poly calc(int m)
{
poly A = Prod(0, m / (k + 1), m);
A = A * binom_poly(m % (k + 1));
return A;
}
poly solve(int m)
{
if (m >= k)
{
poly A = calc(m), B = calc(m - k);
shift(B, k);
for (int i = 0; i < B.size(); ++i)
B[i] = add(0, P - B[i]);
return A + B;
}
return calc(m);
}
int m, a[N], tot = 0;
poly seg[N];
poly prod(int l, int r)
{
if (l == r)
return seg[l];
int mid = (l + r) >> 1;
return prod(l, mid) * prod(mid + 1, r);
}
int main()
{
m = read(), k = read();
fac[0] = 1;
for (int i = 1; i <= m; ++i)
a[i] = read(), fac[i] = mul(fac[i - 1], i);
invfac[m] = fpow(fac[m], P - 2);
for (int i = m - 1; i >= 0; --i)
invfac[i] = mul(invfac[i + 1], i + 1);
sort(a + 1, a + 1 + m);
for (int l = 1, r = 0; l <= m; l = ++r)
{
while (r + 1 <= m && a[r + 1] == a[r] + 1)
++r;
seg[++tot] = solve(r - l + 1);
}
poly cnt = prod(1, tot);
int ans = 0;
for (int i = 0; i < m; ++i)
{
int tmp = fpow(mul(binom(m, i), m - i), P - 2);
tmp = mul(tmp, mul(m, cnt[i]));
inc(ans, tmp);
}
cout << ans << '\n';
return 0;
}