Codeforces 1349F2 Slime and Sequences (Hard Version)

由于心情不好,做了一晚上的 dirty works……

首先是神仙映射。
打个暴力会发现 \(|s_n| = n!\),这意味着一个好序列很可能存在着某种精妙的与排列的映射。
结论:对于 \(n\) 阶排列 \(a\),存在好序列 \(p\),满足: \[ \newcommand{\euler}[2]{ \genfrac{\langle}{\rangle}{0pt}{}{ #1 }{ #2 } } \def\e{ {\rm e} } p_{a_i} = 1+\sum\limits_{j=1}^{i - 1} [a_j < a_{j+1}] \]

\(p\) 为好序列是显然的。
通过察看 \(p_n\dots p_1\) 可以确定 \(n\dots1\) 在原排列 \(a\) 中的的位置,显然这也是唯一对应的。

考虑欧拉数 \(\euler nk\) 表示长度为 \(n\) 的排列,含有 \(k\) 个「升高」的排列个数。
\[ S_k = \sum\limits_{p\in s_n} f_p(k+1) \]

则枚举 \(k+1\) 在原排列中产生的位置,显然 \[ S_k = \sum\limits_{i=\max(1,k)}^n \euler ik \binom ni (n-i)! \]

考虑求出长度为 \(n\) 的序列中钦定 \(k\) 个「升高」的方案数,二项式反演即可得到 \(\euler nk\)
那么考虑到钦定 \(k\) 个「升高」会出现 \(n-k\) 个钦定的「升高」段,升高段内的 EGF 是显然的,因此 \[ \euler nk = n! \sum\limits_{i=\max(1,k)}^n (-1)^{i-k} \binom ik [x^n] (\e^x-1)^{n-i} \]

继续一波代入 \[ \begin{aligned} S_k &= \sum\limits_{i=\max(1,k)}^n \euler ik \binom ni (n-i)! \\ &= \sum\limits_{i=\max(1,k)}^n \binom ni (n-i)! i! \sum\limits_{j=k}^i (-1)^{j-k} \binom jk [x^i] (\e^x-1)^{i-j} \\ &= \frac{n!}{k!} \sum\limits_{j=k}^n \frac{(-1)^{j-k}j!}{(j-k)!} \sum\limits_{i=\max(1,j)}^n [x^i](\e^x-1)^{i-j} \end{aligned} \]

看出了卷积形式,令 \[ W_k = \sum\limits_{i=k}^n [x^i] (\e^x-1)^{i-k} \]

处理一下需要提取系数的项 \[ \begin{aligned} W_k &= \sum\limits_{i=k}^n [x^i] (\e^x-1)^{i-k} \\ &= [x^k] \sum\limits_{i=k}^n \left(\frac{\e^x-1}x\right)^{i-k} \\ &= [x^k] \sum\limits_{i=0}^{n-k} \left(\frac{\e^x-1}x\right)^i \end{aligned} \]

\(F(x) = \frac{\e^x-1}x\),则 \[ W_k = [x^k] \frac{1-F^{n-k+1}(x)}{1-F(x)} = [x^k] \frac1{1-F(x)} - [x^k] \frac{F^{n-k+1}(x)}{1-F(x)} \]

前一项考虑如此处理求逆 \[ [x^k] \frac1{1-F(x)} = [x^{k+1}] \frac x{1-F(x)} \]

后一项先推导亿下 \[ [x^k] \frac{F^{n-k+1}(x)}{1-F(x)} = [x^{n+1}] \frac{ (xF(x))^{n-k+1} }{1-F(x)} \]

鏼爷的论文启发我们使用二元生成函数 \[ [x^{n+1}] \frac{ (xF(x))^{n-k+1} }{1-F(x)} = [x^{n+1}w^{n-k+1}] \frac1{(1-F(x))(1-wxF(x))} \]

那么到此我们将求 \(W_k\) 转为求以上生成函数的 \(x^{n+1}\) 项系数。
尝试探求扩展拉格朗日反演的科技,考虑构造某两个函数的复合 \(H(G(x))\) 恰为以上生成函数,且 \(G(x)\) 的复合逆容易求得。

\(G(x)=xF(x)=\e^x-1\),为了构造 \(H\) 同时需要构造 \(P(G(x))=F(x)\)
显然 \[ P(G(x)) = F(x) \iff P(xF(x)) = F(x) \iff P(x) = \frac{x}{G^{-1}(x)} = \frac x{\ln(1+x)} \]

注意指数上的 \(-1\) 表示函数幂。

从而有 \[ H(x) = \frac1{(1-P(x))(1-wx)} \]

根据扩展拉格朗日反演有 \[ [x^{n+1}] H(G(x)) = \frac1{n+1} [x^n] H'(x) \left(\frac x{G^{-1}(x)}\right)^{n+1} = \frac1{n+1} [x^n] H'(x) P^{n+1}(x) \]


好的那么先来一波喜闻乐见的 dirty works:关于 \(x\)\(H(x)\) 求导: \[ \begin{aligned} H'(x) &= \left(\frac1{(1-P(x))(1-wx)}\right)' \\ &= \left(\frac1{1-P(x)}\right)'\frac1{1-wx}+\frac1{1-P(x)}\left(\frac1{1-wx}\right)' \\ &= \frac{P'(x)}{(1-P(x))^2(1-wx)}+\frac w{(1-P(x))(1-wx)^2} \end{aligned} \]

不用再推下去了,因为接下来只需要这样的形式,刚刚好。

还在本蒟蒻可接受范围之内(


好的那么接下来又是一波 dirty works:xjb 代入 \[ \begin{aligned}[] [x^{n+1}] H(G(x)) &= \frac1{n+1} [x^n] H'(x)P^{n+1}(x) \\ &= \frac1{n+1} [x^n] \left(\frac{P'(x)}{(1-P(x))^2(1-wx)}+\frac w{(1-P(x))(1-wx)^2}\right)P^{n+1}(x) \\ &= \frac1{n+1} [x^n] \left(\frac{P'(x)}{(1-P(x))^2}\sum\limits_{i\ge 0} w^ix^i + \frac1{1-P(x)}\sum\limits_{i\ge 0} (i+1)w^{i+1}x^i\right)P^{n+1}(x) \end{aligned} \]

提取你的系数,令 \(i=n-k+1\)\[ \begin{aligned}[] [w^i x^n] H'(x) P^{n+1}(x) &= [w^i x^n] \left(\frac{P'(x)}{(1-P(x))^2}\sum\limits_{i\ge 0} w^ix^i + \frac1{1-P(x)}\sum\limits_{i\ge 0} (i+1)w^{i+1}x^i\right)P^{n+1}(x) \\ &= [x^n] \left(\frac{P'(x)x^i}{(1-P(x))^2} + \frac{ ix^{i-1} }{1-P(x)}\right)P^{n+1}(x) \\ &= [x^{n-i+2}] \frac{x^2P'(x)P^{n+1}(x)}{(1-P(x))^2}+[x^{n-i+2}] \frac{ixP^{n+1}(x)}{1-P(x)} \\ &= [x^{k+1}] \frac{x^2P'(x)P^{n+1}(x)}{(1-P(x))^2}+[x^{k+1}] \frac{(n-k+1)xP^{n+1}(x)}{1-P(x)} \end{aligned} \]

这东西推出式子之后实现也是很容易出锅的……建议好好整理一下流程再写。

代码:

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
269
#include <cstdio>
#include <vector>
#include <cstring>
#include <utility>
#include <algorithm>
#define add(a,b) (a + b >= mod ? a + b - mod : a + b)
#define dec(a,b) (a < b ? a - b + mod : a - b)
using namespace std;
const int N = 1e5;
const int mod = 998244353;
int n;
int w[N + 5],s[N + 5];
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;
}
namespace Poly
{
const int N = 1 << 18;
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 poly rever() const
{
return poly(vector<int>(a.rbegin(),a.rend()));
}
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 pair<poly,poly> div(poly o) const
{
if(size() < o.size())
return make_pair(poly(),*this);
poly f,g;
f = (rever().modxn(size() - o.size() + 1) * o.rever().inver(size() - o.size() + 1)).modxn(size() - o.size() + 1).rever();
g = (modxn(o.size() - 1) - o.modxn(o.size() - 1) * f.modxn(o.size() - 1)).modxn(o.size() - 1);
return make_pair(f,g);
}
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;
inline int C(int n,int m)
{
return n < m ? 0 : (long long)Poly::fac[n] * Poly::ifac[m] % mod * Poly::ifac[n - m] % mod;
}
poly f,p,pd,p2,pw,t;
poly T1,T2;
int main()
{
init();
scanf("%d",&n),f.resize(n + 2);
for(register int i = 0;i < f.size();++i)
f.a[i] = dec(0,Poly::ifac[i + 2]);
f = f.inver(n + 2);
for(register int i = 0;i <= n;++i)
w[i] = f[i + 1];
p.resize(n + 3);
for(register int i = 0;i < p.size();++i)
p.a[i] = (long long)(i & 1 ? mod - 1 : 1) * Poly::inv[i + 1] % mod;
p = p.inver(n + 3),pd = p.deriv(),pw = p.pow(n + 2,n + 1);
for(register int i = 0;i <= n + 1;++i)
p.a[i] = dec(0,p[i + 1]);
p = p.inver(n + 2),p2 = (p * p).modxn(n + 2);
t = ((p2 * pd).modxn(n + 2) * pw).modxn(n + 2);
for(register int i = 0;i <= n;++i)
w[i] = (w[i] - (long long)t[i + 1] * Poly::inv[n + 1] % mod + mod) % mod;
t = (pw * p).modxn(n + 2);
for(register int i = 0;i <= n;++i)
w[i] = (w[i] - (long long)t[i + 1] * (n - i + 1) % mod * Poly::inv[n + 1] % mod + mod) % mod;
w[0] = dec(w[0],1),T1.resize(n + 1),T2.resize(n + 1);
for(register int i = 0;i <= n;++i)
T1.a[i] = (long long)(i & 1 ? mod - 1 : 1) * Poly::ifac[i] % mod,
T2.a[n - i] = (long long)w[i] * Poly::fac[i] % mod;
T1 = (T1 * T2).modxn(n + 1);
for(register int i = 0;i <= n;++i)
s[i] = (long long)Poly::fac[n] * Poly::ifac[i] % mod * T1[n - i] % mod;
for(register int i = 0;i < n;++i)
printf("%d%c",s[i]," \n"[i == n - 1]);
}