继续数数。
考虑计算 \(i\) 步以后还没有连续 \(k\) 张卡的概率。
容易发现可以转化为对于所有没有连续 \(k\) 张卡的状态,计算 \(i\) 步后到达该状态的概率并求和。
设 \(f_i\) 表示从 \(m\) 张卡里选择 \(i\) 张卡使得没有连续 \(k\) 张卡的方案数。
则答案即 \[
\begin{aligned}
& \sum\limits_{i=0}^{\infty} \sum\limits_{j=0}^{m-1} f_j \sum\limits_{u=0}^j \binom ju (-1)^{j-u} \left(\frac um\right)^i \\
= & \sum\limits_{j=0}^{m-1} f_j \sum\limits_{u=0}^j \binom ju (-1)^{j-u} \sum\limits_{i=0}^{\infty} \left(\frac um\right)^i \\
= & \sum\limits_{j=0}^{m-1} f_j \sum\limits_{u=0}^j \binom ju (-1)^{j-u} \frac m{m-u}
\end{aligned}
\]
后面的和式可以随便卷积一下计算,那么问题转化为计算 \(f_i\)。
容易发现实际上可以将序列 \(a\) 排序后划分为若干极大连续段,对于每个极大连续段分别计算答案再分治 NTT 卷起来。
那么对于一个长度为 \(n\) 的连续段,考虑计算其中的 \(f_i\)。
略加思考后不难发现这个东西就是 \[
[x^{n+1}] \left(\sum\limits_{j=1}^k x^j\right)^{n-i+1}
\]
即,将 \(n\) 张卡的状态视作 \(0/1\);对于每个 \(0\),将其与之前最长的连续 \(1\)(可以为空)合并在一起考虑;那么每一个这样的块的长度便是 \(1\dots k\)。
此外,需要在最后强制加入一个 \(0\)。
那么设 \(G(x) = \frac{x-x^{k+1}}{1-x}\),不难发现,若写出 \(f\) 的生成函数,则有 \[ \sum\limits_{i=0}^n f_i z^i = [x^{n+1}] \frac1{1-G(x)z} \]
考虑对这样一个二元生成函数应用扩展拉格朗日反演,有 \[ [x^{n+1}] \frac1{1-G(x)z} = \frac 1{n+1}[x^n] \frac z{(1-xz)^2} \left(\frac x{G^{-1}(x)}\right)^{n+1} \]
其中 \(G^{-1}\) 为 \(G\) 的复合逆。
若求出 \(G^{-1}\),结合多项式求逆和多项式快速幂并展开 \(\frac z{(1-xz)^2}\) 便可以求得 \(f_i\)。
于是问题变为求解 \(G^{-1}(x)\)。
设 \(F = G^{-1}\),考虑牛顿迭代之。
设已得 \[
G(F_0(x)) \equiv x \pmod{x^n}
\]
在 \(F(x)=F_0(x)\) 处泰勒展开 \[ \begin{aligned} G(F(x)) &\equiv G(F_0(x)) + G'(F_0(x))(F(x) - F_0(x)) \pmod{x^{2n}} \\ F(x) &\equiv F_0(x) + \frac{x-G(F_0(x))}{G'(F_0(x))} \pmod{x^{2n}} \end{aligned} \]
而易得 \[ G'(x) = \frac{kx^{k+1}-(k+1)x^k+1}{(1-x)^2} \]
故倍增 + 多项式全家桶即可(雾
此部分仅有一个 log,算上分治 NTT 总复杂度 \(O(m \log^2 m)\)。
(当然这一个 log 顶人家不知道多少次分治 NTT)
貌似有更优美做法,懒得学了(
生成函数暴力美学(
代码: 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
using namespace std;
const int M = 2e5;
const int mod = 998244353;
inline int fpow(int a,int b)
{
int ret = 1;
for(;b;b >>= 1)
(b & 1) && (ret = (long long)ret * a % mod),a = (long long)a * a % mod;
return ret;
}
int n,m,K;
int a[M + 5];
int len[M + 5],tot;
namespace Poly
{
const int N = 1 << 19;
const int G = 3;
int lg2[N + 5];
int rev[N + 5],fac[N + 5],ifac[N + 5],inv[N + 5];
int rt[N + 5],irt[N + 5];
inline void init()
{
for(register int i = 2;i <= N;++i)
lg2[i] = lg2[i >> 1] + 1;
int w = fpow(G,(mod - 1) / N);
rt[N >> 1] = 1;
for(register int i = (N >> 1) + 1;i <= N;++i)
rt[i] = (long long)rt[i - 1] * w % mod;
for(register int i = (N >> 1) - 1;i;--i)
rt[i] = rt[i << 1];
fac[0] = 1;
for(register int i = 1;i <= N;++i)
fac[i] = (long long)fac[i - 1] * i % mod;
ifac[N] = fpow(fac[N],mod - 2);
for(register int i = N;i;--i)
ifac[i - 1] = (long long)ifac[i] * i % mod;
for(register int i = 1;i <= N;++i)
inv[i] = (long long)ifac[i] * fac[i - 1] % mod;
}
struct poly
{
vector<int> a;
inline poly(int x = 0)
{
x && (a.push_back(x),1);
}
inline poly(const vector<int> &o)
{
a = o,shrink();
}
inline poly(const poly &o)
{
a = o.a,shrink();
}
inline void shrink()
{
for(;!a.empty() && !a.back();a.pop_back());
}
inline int size() const
{
return a.size();
}
inline void resize(int x)
{
a.resize(x);
}
inline int operator[](int x) const
{
if(x < 0 || x >= size())
return 0;
return a[x];
}
inline void clear()
{
vector<int>().swap(a);
}
inline void ntt(int type = 1)
{
int n = size();
type == -1 && (reverse(a.begin() + 1,a.end()),1);
int lg = lg2[n] - 1;
for(register int i = 0;i < n;++i)
rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << lg),
i < rev[i] && (swap(a[i],a[rev[i]]),1);
for(register int w = 2,m = 1;w <= n;w <<= 1,m <<= 1)
for(register int i = 0;i < n;i += w)
for(register int j = 0;j < m;++j)
{
int t = (long long)rt[m | j] * a[i | j | m] % mod;
a[i | j | m] = dec(a[i | j],t),a[i | j] = add(a[i | j],t);
}
if(type == -1)
for(register int i = 0;i < n;++i)
a[i] = (long long)a[i] * inv[n] % mod;
}
friend inline poly operator+(const poly &a,const poly &b)
{
vector<int> ret(max(a.size(),b.size()));
for(register int i = 0;i < ret.size();++i)
ret[i] = add(a[i],b[i]);
return poly(ret);
}
friend inline poly operator-(const poly &a,const poly &b)
{
vector<int> ret(max(a.size(),b.size()));
for(register int i = 0;i < ret.size();++i)
ret[i] = dec(a[i],b[i]);
return poly(ret);
}
friend inline poly operator*(poly a,poly b)
{
if(a.a.empty() || b.a.empty())
return poly();
int lim = 1,tot = a.size() + b.size() - 1;
for(;lim < tot;lim <<= 1);
a.resize(lim),b.resize(lim);
a.ntt(),b.ntt();
for(register int i = 0;i < lim;++i)
a.a[i] = (long long)a[i] * b[i] % mod;
a.ntt(-1),a.shrink();
return a;
}
poly &operator+=(const poly &o)
{
resize(max(size(),o.size()));
for(register int i = 0;i < o.size();++i)
a[i] = add(a[i],o[i]);
return *this;
}
poly &operator-=(const poly &o)
{
resize(max(size(),o.size()));
for(register int i = 0;i < o.size();++i)
a[i] = dec(a[i],o[i]);
return *this;
}
poly &operator*=(poly o)
{
return (*this) = (*this) * o;
}
poly deriv() const
{
if(a.empty())
return poly();
vector<int> ret(size() - 1);
for(register int i = 0;i < size() - 1;++i)
ret[i] = (long long)(i + 1) * a[i + 1] % mod;
return poly(ret);
}
poly integ() const
{
if(a.empty())
return poly();
vector<int> ret(size() + 1);
for(register int i = 0;i < size();++i)
ret[i + 1] = (long long)a[i] * inv[i + 1] % mod;
return poly(ret);
}
inline poly modxn(int n) const
{
if(a.empty())
return poly();
n = min(n,size());
return poly(vector<int>(a.begin(),a.begin() + n));
}
inline poly inver(int m) const
{
poly ret(fpow(a[0],mod - 2));
for(register int k = 1;k < m;)
k <<= 1,ret = (ret * (2 - modxn(k) * ret)).modxn(k);
return ret.modxn(m);
}
inline poly log(int m) const
{
return (deriv() * inver(m)).integ().modxn(m);
}
inline poly exp(int m) const
{
poly ret(1);
for(register int k = 1;k < m;)
k <<= 1,ret = (ret * (1 - ret.log(k) + modxn(k))).modxn(k);
return ret.modxn(m);
}
inline poly pow(int m,int k1,int k2 = -1) const
{
if(a.empty())
return poly();
if(k2 == -1)
k2 = k1;
int t = 0;
for(;t < size() && !a[t];++t);
if((long long)t * k1 >= m)
return poly();
poly ret;
ret.resize(m);
int u = fpow(a[t],mod - 2),v = fpow(a[t],k2);
for(register int i = 0;i < m - t * k1;++i)
ret.a[i] = (long long)operator[](i + t) * u % mod;
ret = ret.log(m - t * k1);
for(register int i = 0;i < ret.size();++i)
ret.a[i] = (long long)ret[i] * k1 % mod;
ret = ret.exp(m - t * k1),t *= k1,ret.resize(m);
for(register int i = m - 1;i >= t;--i)
ret.a[i] = (long long)ret[i - t] * v % mod;
for(register int i = 0;i < t;++i)
ret.a[i] = 0;
return ret;
}
};
}
using Poly::init;
using Poly::poly;
poly f,g[M + 5],h1,h2;
int ans;
inline poly calc(int m)
{
poly ret,powk,powk1,g,d;
for(register int k = 1;k < m;)
k <<= 1,
powk1 = ((powk = ret.pow(k,K)) * ret).modxn(k),
g.resize(2),g.a[0] = 0,g.a[1] = 1,
g -= ((1 - ret).inver(k) * (ret - powk1)).modxn(k),
d = (((1 - ret) * (1 - ret)).inver(k) * (powk1 * K - powk * (K + 1) + 1)).modxn(k),
ret = (ret + g * d.inver(k)).modxn(k);
return ret.modxn(m);
}
poly solve(int l,int r)
{
if(l == r)
return g[l];
int mid = l + r >> 1;
return solve(l,mid) * solve(mid + 1,r);
}
int main()
{
init();
scanf("%d%d",&m,&K),f = calc(m + 2);
for(register int i = 0;i < f.size() - 1;++i)
f.a[i] = f[i + 1];
f.resize(f.size() - 1),f = f.inver(m + 1);
for(register int i = 1;i <= m;++i)
scanf("%d",a + i);
sort(a + 1,a + m + 1),a[0] = -1;
for(register int i = 1;i <= m;++i)
a[i] == a[i - 1] + 1 ? (++len[tot]) : (len[++tot] = 1);
for(register int i = 1;i <= tot;++i)
{
poly t = f.pow(len[i] + 1,len[i] + 1);
g[i].resize(len[i] + 1);
for(register int j = 1;j <= len[i];++j)
g[i].a[len[i] - j + 1] = (long long)Poly::inv[len[i] + 1] * j % mod * t[len[i] - j + 1] % mod;
g[i].a[0] = 1;
}
f = solve(1,tot).modxn(m),h1.resize(m),h2.resize(m);
for(register int i = 0;i < m;++i)
h1.a[i] = (long long)(i & 1 ? mod - 1 : 1) * Poly::ifac[i] % mod,
h2.a[i] = (long long)m * Poly::inv[m - i] % mod * Poly::ifac[i] % mod;
h1 = (h1 * h2).modxn(m);
for(register int i = 0;i < m;++i)
ans = (ans + (long long)f[i] * h1[i] % mod * Poly::fac[i] % mod) % mod;
printf("%d\n",ans);
}