A - XOR Cross Over 解説 by evima
The operation splits a sequence and adds the total \(\mathrm{XOR}\) of each part to each element of the other part, continuing until all sequences have length \(1\).
When a sequence is split into two parts of odd + odd length or odd + even length, the influence that has affected every element of the sequence vanishes (for example, when splitting \((a \oplus X, b\oplus X, c\oplus X, d\oplus X)\) into odd + odd lengths, the result does not depend on \(X\)). Focusing on this fact, we rephrase the operation as follows.
First, consider splitting an even-length sequence into two even-length parts. Will the mutual influence caused by this split vanish later? Ultimately, the sequence is split until we reach length \(1\) (which is odd), meaning there will definitely be a stage where it is split into odd + odd. Hence, any influence caused by the initial even + even split must always vanish. In other words, we can treat an even + even split as merely “splitting the sequence.”
Next, consider splitting an even-length sequence into two odd-length parts. An odd-length sequence, when split, always produces another odd-length part, so in the end there will remain exactly two elements that have stayed within an odd-length sequence from start to finish. Because odd-length parts can only split off an even-length part, the influence on these elements (i.e., the original total \(\mathrm{XOR}\)) does not vanish. For all other elements, that influence has vanished upon splitting the odd-length sequence into odd + even, so again it acts effectively like just splitting the sequence.
Summarizing these points for the case when \(N\) is even, we can re-interpret the operation as follows, applying it only to sequences of even length:
- Splitting a sequence into two even-length sequences.
- Choose integers \(l \leq i < j \leq r\) such that \(i-l\) and \(r-j\) are both even. Replace \(A_i, A_j\) with \(A_l \oplus A_{l+1} \oplus \dots \oplus A_r\). Then, split the remaining part into three sequences.
We can compute the minimum possible sum of \(A\) after performing these operations by using an interval DP in \(O(N^3)\) time. (Naively considering the second type of operation can lead to \(\Theta(N^4)\) time, but for example, by keeping track of how many times you have already done an assignment \((0,1,2)\) as part of the state, you can reduce it to \(O(N^3)\).)
When \(N\) is odd, we can simply brute force over the positions where \(A_1 \oplus A_2 \oplus \dots \oplus A_N\) might be assigned.
投稿日時:
最終更新: