Official

E - Xor Annihilation Editorial by evima


Let us compute the sequence of \(L\) and \(R\) for the balls. Consider the sequence of cumulative XOR sums of the weights, including \(Z\) at both ends. The total XOR of the weights is \(0\), so \(L\) and \(R\) for a ball correspond to the terms that are just before and after that ball, respectively. Next, let us see what happens when two balls come to the same coordinate. This corresponds to the disappearance of the term between those two balls in the above sequence of cumulative XOR sums. Additionally, from the rules of the moves, one can see that each ball moves toward the larger of the two adjacent terms in the sequence of cumulative XOR sums.

Thus, one can simulate the moves of the balls by deleting a term that is larger than the two adjacent terms (accurately, smaller than neither of them, and larger than at least one of them) in the sequence of cumulative XOR sums and concatenating the remaining parts. The balls come to rest if and only if all terms in that sequence at that point are equal. This will happen if and only if that sequence contains no term smaller than \(Z\) at both ends.

Therefore, the problem is equivalent to the following: “We have the sequence \(p_i\) of cumulative XOR sums for \(Z=0\). Find the number of integers \(Z\) at most \(2^N-1\) such that \(Z\) xor \(p_i\) is never smaller than \(Z\).” We have \(Z>(Z\) xor \(p_i)\) when the most significant bit of \(p_i\) is present in \(Z\). Thus, if there are \(k\) bit positions where none of the \(p_i\) has its most significant bit, the answer is \(2^k\).

posted:
last update: