Loading

Infinite Sequence (Easy Version)

思路

题意

给定一个正整数 nn 和一个无限二进制序列 aa 的前 nn 项, 该序列定义如下:
对于 m>nm > n, am=a1a2am2a_m = a_1 \oplus a_2 \oplus \ldots \oplus a_{\lfloor \frac{m}{2}\rfloor}

tt 次询问 (t104)(t \leq 10^4) , 求 aia_i 值, 其中 1i10181 \leq i \leq 10^{18}
其中 ai{0,1}a_i \in \{0, 1\}

这不用找规律?
试图矩阵快速幂

\[\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)) \]

总结

异或性质

posted @ 2025-03-05 19:16  Yorg  阅读(38)  评论(0)    收藏  举报