Official

E - Adjacent Xor Game Editorial by evima


Take \(L\) such that \(A_i < 2^L\).

Below, we consider only Bob’s perspective, assuming \(x_i\) as the input. The problem can be rephrased as follows:

Add to \(x\) any number of non-negative integers of the form \(2^p-1\), creating a new set \(v\). Rearrange \(v_i\) appropriately, and let \(y'_i\) be the cumulative XOR. Here, \(y'_i\) must be non-decreasing. The score is the last term of \(y'_i\).

The validity of the rephrasing can be seen as follows: First, to obtain \(y\) from \(y'\), we just need to extract the \(y'_i\) before and after the term derived from \(x_i\) and set it as \(y\). Conversely, to obtain \(y'\) from \(y\), we divide the interval between \(y_{2i}\) and \(y_{2i+1}\) into \(y_{2i}, y_{2i}+1, ..., y_{2i+1}\).

How to solve the rephrased problem: First, when \(v\) is fixed, determine if \(y'\) can be made to satisfy the condition. (Since the last term of \(y'\) does not depend on the rearrangement of \(v\), it is the determination problem that matters.)

It is sufficient to make the prefix XOR of \(v_i/2^p\) (the integer part) non-decreasing for all non-negative integers \(p=0,1,\cdots\). Suppose we have a sequence that satisfies the condition for \(v_i/2^{p+1}\). From here, let’s try to create a solution for \(v_i/2^p\). For \(v_i\) such that \(v_i/2^p \geq 2\), if they are arranged in the same order as in the solution for \(v_i/2^{p+1}\), they also satisfy the condition for \(v_i/2^p\). The rest is to cleverly insert into this \(v_i\) such that \(v_i/2^p=1\) to keep the prefix XOR non-decreasing. It is easy to determine if this is possible. Let \(a\) be the count of \(v_i\) such that \(v_i/2^p \geq 2\) and \(v_i/2^p\) is odd, and \(b\) be the count of \(v_i\) such that \(v_i/2^p =1\). Then, we just need to see if \(b \leq a+1\). If we consider adding \(v_i\) such that \(v_i/2^p=1\) every time the prefix XOR of \(v_i/2^p\) becomes even, we can always construct a sequence that satisfies the condition if \(b \leq a+1\). Conversely, if \(b>a+1\), we cannot construct it no matter what.

We have now solved the problem when \(v\) is fixed. The rest is to move \(v\) around and consider the problem of minimizing the total XOR (let’s call it \(d\)) while keeping the answer to the determination problem true.

We consider the determination for a fixed \(d\). Consider matching the conditions from the upper bits. Let \(d_p\) be the \(p\)-th bit from the bottom of \(d\).

First, set \(v=x\). For \(p=60,59,\cdots\), focus on the \(p\)-th bit and add values to \(v\). Let \(a_p\) be the count of \(x_i\) such that \(x_i/2^p \geq 2\) and \(x_i/2^p\) is odd, \(b_p\) be the count of \(x_i\) such that \(x_i/2^p =1\), and \(c_p\) be the count of numbers we have decided to add so far. We consider how many numbers with the most significant bit at the \(p\)-th bit (\(=2^{p+1}-1\)) to add. Considering that adding as many as possible would work to our advantage later and that the XOR matches \(d_p\), we can see that we should add \(a_p+c_p-b_p+d_p\). If this number is negative, we can determine that this \(d\) is unachievable.

The inequality of the form \(a_p+c_p-b_p+d_p \geq 0\) ends up mattering only in the range \(p < L\). Let’s further simplify these \(L\) inequalities. The values \(a_p\) and \(b_p\) can be immediately calculated from the input \(x_i\). Also, using the relationship \(c_p+(a_p+c_p-b_p+d_p)=c_{p-1}\), we can see that all \(c_p\) can be written as the sum of \(a_j, b_j, d_j\). After all, each inequality takes the form \(d_{p}+d_{p+1}+2d_{p+2}+4d_{p+3}+\cdots \geq e_p\) (\(e_p\) is a value that can be calculated from the input \(x_i\)). Further transforming this, we get \(d \geq 2^p(2e_p-1)\). Eventually, \(d\) is the max of \(2^p(2e_p-1)\).

Now that we are done with Bob, let’s consider Alice’s strategy.

\(e_p\) is calculated from \(x\), and it is clear that each \(x_i\) contributes independently. In other words, a value \(e_{i,p}\) can be calculated from \(x_i\), and we have \(e_p=\sum_{1 \leq i \leq k} e_{i,p}\).

Therefore, as Alice’s strategy, after calculating all \(e_{i,j}\) corresponding to each \(A_i\), for each \(p\), arrange \(e_{i,p}\) in descending order, and try taking the first \(k\).

The complexity is \(O(NL\log(N))\).

Sample Implementation

posted:
last update: