题解 P8554【心跳】(魔怔题解)
考虑找出合法的
我们考虑原排列中的每个前缀最大值都“控制”它后面的一段:也即,那一段内的元素都小于这个前缀最大值,这个控制的区间向右延伸到下一个前缀最大值前(或排列末尾)。
记原排列的前缀最大值个数为
当删去前缀最大值时,这个前缀最大值所控制的段内其他元素都将暴露在外,如果这些元素中出现了新的
记这一段(包含它自己)的长度为
- 答案几乎是肯定的:除了在第一段中如果
h \ge 2 就无法取到a_1 = k - 1 外,总能构造一个排列p 使其恰好得到序列a 。 - 我们可以感性证明一下这个结论。注意,在原排列中,“当删去前缀最大值时,这个前缀最大值所控制的段内其他元素都将暴露在外”。只要我们可以自由控制这
h - 1 个元素暴露在外后,恰好出现的前缀最大值的数量(即在[0, h - 1] 中任取),就能达到根据每段长度和每个a_i 反向构造原排列的目的。 - 对于所有段,可以发现,总是可以排列段内其他元素的相对顺序(段内所有元素都大于上一段的最大值),达到恰好新出现
[1, h - 1] 个前缀最大值。 - 要达到恰好新出现
0 个前缀最大值,即其他元素全部被前一段的最大值隐藏,前提是“存在前一段”或h = 1 ,即当第一段的h \ge 2 时,不可能取到a_1 = k - 1 。
我们恰恰是要对序列
-
-
- 注意它们的段分布情况不同,但是对应的序列
a 相同,所以不能通过段分布情况对所有可能的序列a 直接进行分类。
为了进一步考虑对不同的段分布情况的序列
已知序列
考察序列
-
-
-
-
-
- 以此类推……
回顾序列
- 我们可以把每一段看作
[-1], [0, 0], [1, 0, 0], [2, 0, 0, 0], \ldots 之一与若干个0 拼接而成。 - 记段的组合类为
\mathcal{S} ,则\displaystyle \operatorname{OGF}(\mathcal{S}) = \frac{x}{1 - x} \cdot \frac{1}{1 - x} 。 - 注意第一段不能以
[-1, 0] 为前缀,只能是[-1] 或以其他数开头,所以记第一段的组合类为\mathcal{S}_1 时有\displaystyle \operatorname{OGF}(\mathcal{S}_1) = x + \frac{x^2}{1 - x} \cdot \frac{1}{1 - x} 。 - 那么附加了段分布信息的序列
b 的组合类就是\mathcal{S}_1 \times \operatorname{SEQ}(\mathcal{S}) ,有 OGF\displaystyle \frac{x + \dfrac{x^2}{1 - x} \cdot \dfrac{1}{1 - x}}{1 - \dfrac{x}{1 - x} \cdot \dfrac{1}{1 - x}} 。
但是,我们需要计数的并不是附加上段结构信息的序列
- 相同的序列
b ,如果有不同的k (即段数),当然会对应到不同的序列a 上。 - 相同的序列
b ,如果有不同的段结构信息,但段数k 相同,也会对应到相同的序列a 上。
即,从序列
-
-
-
- 前两个
\langle b, k \rangle 需要被视为同一类,但第三个\langle b, k \rangle 有着不同的k ,需要分开计数。
注意,这里默认了不同的序列
通过上述例子,容易观察到出现重复的
- 如果强制不能出现“形如
[0, 0] 后跟若干个0 的段”,即是每一段的首位都非零,可以直接根据序列b 本身的值唯一确定段分布信息,故也确定了段数。这就是说,直接将[-1], [1, 0, 0], [2, 0, 0, 0], \ldots 之一与若干个0 拼接而成的段进行不限数量的有序拼接(\operatorname{SEQ} )即可不重不漏地枚举到所有序列b 。 - 翻译成符号化方法的语言即是,
\displaystyle \operatorname{OGF}(\mathcal{S}) = \left( \frac{x}{1 - x} - x^2 \right) \cdot \frac{1}{1 - x} 和\displaystyle \operatorname{OGF}(\mathcal{S}_1) = x + \frac{x^3}{1 - x} \cdot \frac{1}{1 - x} 。 - 而不允许出现
0 开头的段的序列b 的组合类即是\mathcal{S}_1 \times \operatorname{SEQ}(\mathcal{S}) ,有 OGF\displaystyle \frac{x + \dfrac{x^3}{1 - x} \cdot \dfrac{1}{1 - x}}{1 - \left( \dfrac{x}{1 - x} - x^2 \right) \cdot \dfrac{1}{1 - x}} 。
但是如果考虑上首位为
-
不带段分布信息地给定一个合法序列
b ,它能对应的合法k 必须是一个区间。感性理解: -
区间必须有端点
k \in [l, r] ,我们可以构造性地指出端点和中间所有数都能被k 取到。 -
显然每个非零数都必须在不同的段中,不妨先粗暴地把每个非零数与其后面的
0 连续段划分为同一段。序列b 以0 或-1 开头时进行一些特别处理。举一些例子:- 将
b = [2, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, -1, 0, 0, 0, 1, 0, 0] 划分为[\underline{2, 0, 0, 0, 0, 0, 0, 0}\ ,\ \underline{1, 0, 0, 0}\ ,\ \underline{-1, 0, 0, 0}\ ,\ \underline{1, 0, 0}] 。 - 将
b = [0, 0, 0, 0, 0, 2, 0, 0, 0, -1, 0, 1, 0, 0, 0, -1] 划分为[\underline{0, 0, 0, 0, 0}\ ,\ \underline{2, 0, 0, 0}\ ,\ \underline{-1, 0}\ ,\ \underline{1, 0, 0, 0}\ ,\ \underline{-1}] 。 - 将
b = [-1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, -1, 0, 0, 0] 划分为[\underline{-1}\ ,\ \underline{0, 0, 0, 0, 0}\ ,\ \underline{1, 0, 0, 0, 0, 0}\ ,\ \underline{-1, 0, 0, 0}] 。
总的来说,即是保证每一段在合法的前提下尽量长。可以看出,这种划分唯一且一定是合法的,否则就不存在任何合法划分了。这样导出的段分布一定是段数最少的,此时即为
k = l ,取到区间的左端点。 - 将
-
对于右端点,考虑划分段数最多的方式。如果这一段的首元素为
v ,则段必须至少长度为v + 2 ,但是如果在前一种划分方式中段长为h ,即是说在段的末尾超出了h - (v + 2) 个0 。这写多余的0 ,为了使段数尽量多,可以两个两个结合成段,即产生\left\lfloor \dfrac{h - (v + 2)}{2} \right\rfloor 个新的段,按相同例子:- 将
b = [2, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, -1, 0, 0, 0, 1, 0, 0] 划分为[\underline{2, 0, 0, 0}\ ,\ \underline{0, 0}\ ,\ \underline{0, 0}\ ,\ \underline{1, 0, 0, 0}\ ,\ \underline{-1, 0}\ ,\ \underline{0, 0}\ ,\ \underline{1, 0, 0}] ,k 增加了3 。 - 将
b = [0, 0, 0, 0, 0, 2, 0, 0, 0, -1, 0, 1, 0, 0, 0, -1] 划分为[\underline{0, 0, 0}\ ,\ \underline{0, 0}\ ,\ \underline{2, 0, 0, 0}\ ,\ \underline{-1, 0}\ ,\ \underline{1, 0, 0, 0}\ ,\ \underline{-1}] ,k 增加了1 。 - 将
b = [-1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, -1, 0, 0, 0] 划分为[\underline{-1}\ ,\ \underline{0, 0, 0}\ ,\ \underline{0, 0}\ ,\ \underline{1, 0, 0, 0}\ ,\ \underline{0, 0}\ ,\ \underline{-1, 0}\ ,\ \underline{0, 0}] ,k 增加了3 。
这种满足段数尽量多的划分不一定唯一,如果存在
h - (v + 2) 为奇数且\ge 3 ,则可以有多种具体的段分布方式。相比k 尽量少的划分,k 一般会增加,但也可能无法增加,即可能存在每一段都出现了h - (v + 2) \le 1 的情况。 - 将
-
由于划分段数最多是基于划分段数最少的方法进一步划分而成,显然中间的每个数都能被
k 取到,只需进行不完全的划分即可。
既然已知不带附加信息的合法序列
即,我们试图求出,
- 对应到的
k 区间的左端点恰好为l 的序列b 的个数,记作f(n, l) 。 - 对应到的
k 区间的右端点恰好为r 的序列b 的个数,记作g(n, r) 。
如果记
现在只考虑求
对于
-
有
\displaystyle \operatorname{OGF}(\mathcal{S}) = y \cdot \left[ \left( \frac{x}{1 - x} - x^2 \right) \cdot \frac{1}{1 - x} \right] 。 -
对于首(两)段的特殊处理,有
\displaystyle \operatorname{OGF}(\mathcal{S}_1) = y \cdot \left( x + x \cdot y \cdot \frac{x^2}{1 - x} + \frac{x^2}{1 - x} \cdot \frac{1}{1 - x} \right) 。这是因为首段可以以0, 1, 2, \ldots 开头,而-1 开头时,要么不能往后接0 了,要么后面的0 至少接两个,并独立成段。 -
最后,有
\mathcal F = \mathcal{S}_1 \times \operatorname{SEQ}(\mathcal{S}) 。对于 OGF 有 -
\begin{aligned} F(x, y) = \operatorname{OGF}(\mathcal{F}) &= \frac{\operatorname{OGF}(\mathcal{S}_1)}{1 - \operatorname{OGF}(\mathcal{S})} \\ &= \frac{y \cdot \left( x + x \cdot y \cdot \dfrac{x^2}{1 - x} + \dfrac{x^2}{1 - x} \cdot \dfrac{1}{1 - x} \right)}{1 - y \cdot \left[ \left( \dfrac{x}{1 - x} - x^2 \right) \cdot \dfrac{1}{1 - x} \right]} \\ \text{(WolframAlpha or Mathematica)\qquad} &= \frac{x y - x^2 y + x^3 y + x^3 y^2 - x^4 y^2}{1 - (2 x - x^2 + y \cdot (x - x^2 + x^3))} \text{.} \end{aligned}
对于
-
有
\displaystyle \operatorname{OGF}(\mathcal{S}) = \left[ y \cdot (1 + x) \cdot \left( \frac{x}{1 - x} - x^2 \right) \right] \cdot \frac{1}{1 - y \cdot x^2} 。此处相当于以基本的\displaystyle \frac{x}{1 - x} - x^2 为基础,先乘上(1 + x) 表示多出奇数个0 与偶数无异,然后乘上y 表示这一段本身的贡献,后面乘上\operatorname{SEQ}(y \cdot x^2) 表示每两个0 单独成段。 -
对于首(若干)段的特殊处理,有
\displaystyle \operatorname{OGF}(\mathcal{S}_1) = y \cdot x + \left[ y \cdot (1 + x) \cdot \left( x \cdot y \cdot x^2 + \frac{x^2}{1 - x} \right) \right] \cdot \frac{1}{1 - y \cdot x^2} 。其中在外的y \cdot x 对应首段为-1 并不接任何0 的情况,内部的x \cdot y \cdot x^2 对应首(两)段为[-1] 、[0, 0] 的情况,它们也需要乘(1 + x) 和\displaystyle \frac{1}{1 - y \cdot x^2} 。 -
最后,有
\mathcal G = \mathcal{S}_1 \times \operatorname{SEQ}(\mathcal{S}) 。对于 OGF 有 -
\begin{aligned} G(x, y) = \operatorname{OGF}(\mathcal{G}) &= \frac{\operatorname{OGF}(\mathcal{S}_1)}{1 - \operatorname{OGF}(\mathcal{S})} \\ &= \frac{y \cdot x + \left[ y \cdot (1 + x) \cdot \left( x \cdot y \cdot x^2 + \dfrac{x^2}{1 - x} \right) \right] \cdot \dfrac{1}{1 - y \cdot x^2}}{1 - \left[ y \cdot (1 + x) \cdot \left( \dfrac{x}{1 - x} - x^2 \right) \right] \cdot \dfrac{1}{1 - y \cdot x^2}} \\ \text{(WolframAlpha or Mathematica)\qquad} &= \frac{x y + x^3 y + x^4 y^2 - x^5 y^2}{1 - (x + y \cdot (x + x^2 - x^3 + x^4))} \text{.} \end{aligned}
接下来,根据
根据
仍要注意,直接计算
-
在记号上,将禁止
-1 的相关对象加个星号。类似地,我们要求F^{{\ast}}(x, y) 对应左端点、以及G^{{\ast}}(x, y) 对应右端点。 -
对于
F^{{\ast}}(x, y) ,有\displaystyle \operatorname{OGF}(\mathcal{S}) = y \cdot \frac{x^3}{1 - x} \cdot \frac{1}{1 - x} 和\displaystyle \operatorname{OGF}(\mathcal{S}_1) = y \cdot \frac{x^2}{1 - x} \cdot \frac{1}{1 - x} 。计算可得F^{{\ast}}(x, y) = \frac{\operatorname{OGF}(\mathcal{S}_1)}{1 - \operatorname{OGF}(\mathcal{S})} = \cdots = \frac{x^2 y}{1 - (2 x - x^2 + y \cdot (x^3))} \text{.} -
对于
G^{{\ast}}(x, y) ,有\displaystyle \operatorname{OGF}(\mathcal{S}) = \left[ y \cdot (1 + x) \cdot \frac{x^3}{1 - x} \right] \cdot \frac{1}{1 - y \cdot x^2} 和\displaystyle \operatorname{OGF}(\mathcal{S}_1) = \left[ y \cdot (1 + x) \cdot \frac{x^2}{1 - x} \right] \cdot \frac{1}{1 - y \cdot x^2} 。计算可得G^{{\ast}}(x, y) = \frac{\operatorname{OGF}(\mathcal{S}_1)}{1 - \operatorname{OGF}(\mathcal{S})} = \cdots = \frac{x^2 y + x^3 y}{1 - (x + y \cdot (x^2 + x^4))} \text{.}
那么,类似地,我们有
答案即为
为了追求更好的时间复杂度,注意到答案为
再结合这一事实——对关于
这启发我们对答案中的二元有理分式进行部分分式分解,得到分母中
对
基于这些结果,通过先提取
或者结合