思路
题意
给定一个正整数 和一个无限二进制序列 的前 项, 该序列定义如下:
对于 ,
次询问 , 求 值, 其中
其中
这不用找规律?
试图矩阵快速幂
\[\begin{align*}
\begin{cases}
a_i \gets a_{i - 1} \oplus a_{\frac{i}{2}} , \textrm{ case } i \textrm{ mod } 2 = 0 \\
a_i \gets a_{i - 1} , \textrm{ otherwise}
\end{cases}
\end{align*}\]
倘若维护
\[\begin{pmatrix}
a_i \\
a_{\frac{i + 1}{2}}
\end{pmatrix}
\]
在更新 \(a_{\frac{i + 1}{2}}\) 需要其他信息, 因此不行
哎哎哎, 看了题解怎么是暴力优化
发现异或操作的性质是: 两个相同数异或可以无视
为了方便起见, 假设 \(n\) 是奇数(如果不是,递增 \(n\) 共单独处理边界情况)。首先预计算前 \(2n\) 项或等于 \(2n\) 的查询,直接返回预计算的值。对于 \(2m > n\),观察以下关系:
\[a_{2m} = a_1 \oplus a_2 \oplus \ldots \oplus a_m = a_{2m+1}
\]
定义 \(p = a_1 \oplus a_2 \oplus \ldots \oplus a_n\),我们可以将异或和分解为:
\[a_{2m} = a_1 \oplus a_2 \oplus \ldots \oplus a_m = a_1 \oplus a_2 \oplus \ldots \oplus a_n \oplus (a_{n+1} \oplus a_{n+2}) \oplus (a_{n+3} \oplus a_{n+4}) \oplus \ldots \oplus a_m
\]
由于 \(n\) 是奇数,成对的 \((a_{n+1} \oplus a_{n+2})\), \((a_{n+3} \oplus a_{n+4})\), \(\ldots\),会相互抵消。这简化了公式为:
\[a_{2m} = a_{2m+1} =
\begin{cases}
p & \textrm{case } m \textrm{ mod } 2 = 1 \\
p \oplus a_m & \textrm{otherwise }
\end{cases}
\]
因此,我们可以通过递归地将 \(m\) 减半来计算 \(a_m\),直到 \(m \leq 2n\),并在每一步应用奇偶性规则。总体时间复杂度为:
\[O(\log(m))
\]
总结
异或性质