题解:CF1864H Asterism Stream

· · 题解

由于 Markdown 过于石山(特别是转移矩阵),使用 DeepSeek 帮忙写了一下。

思路

设 p_i 为从 1 出发到 i 的概率,显然期望 E = \sum_{i = 1}^{n - 1} p_i。

显然,p_i 的递推式为:

p_i = \left\{\begin{matrix} \frac{1}{2}p_{i - 1}, &i\bmod 2 = 1 \\ \frac{1}{2}p_{i - 1} + \frac{1}{2}p_{\frac{i}{2}}, &i\bmod 2 = 0 \end{matrix}\right.

初始化 p_0 = 2,也就是令 p_1 = 1。

观察到 n\le 10^{18},而我们又要求一个递推式,显然考虑矩阵快速幂优化。

考虑怎么设计状态矩阵。显然,需要维护 p_{\left \lfloor \frac{x}{2} \right \rfloor },同时需要维护转移到 p_{\left \lfloor \frac{(x + 1)}{2} \right \rfloor },那么又要存下 p_{\left \lfloor \frac{x}{4} \right \rfloor },以此类推。

于是设计状态矩阵:

V_x = \begin{bmatrix} p_x, p_{\left \lfloor \frac{x}{2} \right \rfloor }, p_{\left \lfloor \frac{x}{4} \right \rfloor },\dots ,p_{\left \lfloor \frac{x}{2^{59}} \right \rfloor},s_x \end{bmatrix}

其中 s_x = \sum_{i = 1}^x p_x。

考虑从 x 转移到 x + 1,当 \text{lowbit}(x + 1) = 2^i 时,对于矩阵的前 i + 1 个分量(其中 j 从 0 到 1):

p_{\left \lfloor \frac{x + 1}{2^j} \right \rfloor} = \sum_{k = j}^i\frac{1}{2^{k - j + 1}}\times p_{\left \lfloor \frac{x}{2^k} \right \rfloor}

对于其余分量

p_{\left \lfloor \frac{x + 1}{2^j} \right \rfloor} = p_{\left \lfloor \frac{x}{2^j} \right \rfloor}

对于 s_x,加上新的 p 即可。

那么,转移矩阵就是:

\begin{pmatrix} \frac{1}{2} & \frac{1}{4} & \frac{1}{8} & \cdots & \frac{1}{2^{i+1}} & 0 & \cdots & 0 & 1 \\ 0 & \frac{1}{2} & \frac{1}{4} & \cdots & \frac{1}{2^{i}} & 0 & \cdots & 0 & 0 \\ 0 & 0 & \frac{1}{2} & \cdots & \frac{1}{2^{i-1}} & 0 & \cdots & 0 & 0 \\ \vdots & \vdots & \vdots & \ddots & \vdots & \vdots & & \vdots & \vdots \\ 0 & 0 & 0 & \cdots & \frac{1}{2} & 0 & \cdots & 0 & 0 \\ 0 & 0 & 0 & \cdots & 0 & 1 & \cdots & 0 & 0 \\ \vdots & \vdots & \vdots & & \vdots & \vdots & \ddots & \vdots & \vdots \\ 0 & 0 & 0 & \cdots & 0 & 0 & \cdots & 1 & 0 \\ 0 & 0 & 0 & \cdots & 0 & 0 & \cdots & 0 & 1 \end{pmatrix}

最后,将 n 二进制分解,再 2^i 步 2^i 步这样转移即可。

至于前 2^i 项的积,注意到是中心两边相等的,求出来也是简单的。

代码