CF2071D2

题目大意:

有一个无限的 0/1 数组 \(a\)\(a_{1} \sim a_{n}\) 已经给定,对于 \(i > n\),有

\[a_{i} = \oplus_{j = 1}^{\lfloor \frac{i}{2} \rfloor} a_{j} \]

\(\sum_{i = l} ^ {r} a_{i}\)
\(n \le 2 \times 10^5,1 \le l \le r \le 10^{18}\)

解题思路:

首先转化成 \(\sum_{i = 1}^r a_{i} - \sum_{i = 1}^{l - 1} a_{i}\) 的形式。
那么考虑 \(\sum_{i = 1}^{x} a_{i}\) 怎么求。
然后我就不会了...
下面需要一个性质:若 \(i\) 为偶数且 \(i > n\),那么 \(a_{i} = a_{i + 1}\)
为了方便,将 \(n\) 补为奇数。
又由于异或的性质:\(x \oplus x = 0\),当 \(i > n\) 时有:

\[\oplus_{j = 1}^{i} a_{j} = \oplus_{j = 1}^{n} a_{j} \oplus ([2 \nmid i] \times a_{i}) \]

\(\sum_{i = 1}^{x} a_{i}\) 转化:

\[\sum_{i = 1}^{x} a_{i} = \sum_{i = 1}^{2 \times n + 1} a_{i} + \sum_{i = 2 \times n + 2}^{x} a_{i} \]

将好求的扔掉,设 \(s = \oplus_{i = 1}^n a_{i}\) 再将后面的式子拆开一下:

\[\sum_{i = 2 \times n + 2}^{x} a_{i} = \sum_{i = n + 1}^{\lfloor \frac{x}{2} \rfloor} \oplus_{j = 1}^{i} a_{j} + \sum_{i = n + 1}^{\lfloor \frac{x - 1}{2} \rfloor} \oplus_{j = 1}^{i} a_{j} \]

发现这两个式子形式基本相同,于是设 \(f(x) = \sum_{i = n + 1}^{x} \oplus_{j = 1}^{i} a_{j}\)
通过前面的

\[\oplus_{j = 1}^{i} a_{j} = \oplus_{j = 1}^{n} a_{j} \oplus ([2 \nmid i] \times a_{i}) \]

我们将 \(f\) 中的 \(i\) 分奇偶讨论:

\[f(x) = \sum_{i = n + 1}^{x} \oplus_{j = 1}^{i} a_{j} = \sum_{i = n + 1}^{x} \oplus_{j = 1}^n a_{j} \oplus \oplus_{j = n + 1}^{i} a_{j} = \sum_{i = n + 1, 2 \mid i}^{x} s \oplus a_{i} + \sum_{i = n + 1, 2 \nmid i}^{x} s \]

后面的式子时好求的,因为 \(n\) 为奇数,所以 \(n + 1 \sim k\) 中奇数的数量为

\[\lfloor \frac{k - n}{2} \rfloor \]

所以将它扔掉。

\[\sum_{i = n + 1, 2 \mid i}^{x} s \oplus a_{i} = \sum_{i = \lfloor \frac{n + 1}{2} \rfloor}^{\lfloor \frac{x}{2} \rfloor} s \oplus \oplus_{j = 1}^{i} a_{j} \]

拆一下:

\[\sum_{i = \lfloor \frac{n + 1}{2} \rfloor}^{\lfloor \frac{x}{2} \rfloor} s \oplus \oplus_{j = 1}^{i} a_{j} = \sum_{i = \lfloor \frac{n + 1}{2} \rfloor}^{n} s \oplus \oplus_{j = 1}^{i} a_{j} + \sum_{i = n + 1}^{\lfloor \frac{x}{2} \rfloor} s \oplus \oplus_{j = 1}^{i} a_{j} \]

那么你发现前面的式子也是可以预处理的,而后面的式子相当于 \(f(\lfloor \frac{n}{2} \rfloor)\) 再异或个 \(s\)
\(s = 0\) 时,结果不变,否则全体取反。
由于每次会将 \(\frac{x}{2}\),那么时间复杂度为 \(O(n + \log V)\)

这题可以说是用到了几乎所有的异或性质吧

posted @ 2025-04-14 22:01  positive_deviation  阅读(32)  评论(0)    收藏  举报