Official

E - Adjacent XOR Editorial by evima


Let \(M\) be the maximum value in \(A, B\). From the Constraints, \(M< 2^{60}\).


[1] Looking it backward

By reversing the process of the operation, one can assume that the following operation is available instead of the given one:


  • Choose an integer \( K\ (1\le K \le N)\). For every \(i\ (1\leq i \leq K)\), simultaneously replace the value of \( B_i\) with \( B_1\oplus B_2 \oplus \ldots \oplus B_i\). —

Below, we consider this version of the problem.


[2] Describing the possible states

Let us characterize the sequence \(B\) after some operations.

Let \(B'\) be the sequence \(B\) after zero or more operations. It can be seen that the following holds for each \(i\):

\[ B'_i = B_i \oplus (\text{the XOR of some subset of }\{B_1,B_2,\ldots,B_{i-1}\}).\]

This can be proved by mathematical induction.

Therefore, the objective is achievable if and only if, for each \(i\), \(A_i\) can be represented as \(B_i \oplus (\text{the XOR of some subset of }\{B_1,B_2,\ldots,B_{i-1}\})\).

This condition can be checked in \(O(N\log M)\) time by Gaussian elimination.

In fact, if this condition is satisfied, one can always match \(B\) to \(A\) in at most \(70000\) operations, as explained below.


[3] The specific procedure

Below, assume that the necessary and sufficient condition explained above is satisfied.

If one chooses \(K\) in the operation, \(B_{K+1}\) and subsequent elements will not be affected, so it seems reasonable to try matching \(B\) to \(A\) from the end to the beginning. If we can match the last elements in \(\mathrm{O}(\log M)\) operations, repeating it \(N\) times will match the whole sequences in \(\mathrm{O}(N\log M)\) operations, which will be at most \(70000\).

Now, let us find a way to match \(A_i\) and \(B_i\) in \(\mathrm{O}(\log M)\) operations.

Since we are matching the sequences from the end to the beginning, we cannot choose \(K\) greater than \(i\). Ignoring the trivial case \(A_i=B_i\), we have to choose \(K=i\) at the end of the process of making \(A_i=B_i\).

An operation with \(K=i\) makes \(B_i = B_1\oplus B_2 \oplus \ldots \oplus B_{i-1} \oplus B_i\), so we want to match \(B_1\oplus B_2 \oplus \ldots \oplus B_{i-1} \oplus B_i\) to \(A_i\) (or make \(B_1\oplus B_2 \oplus \ldots \oplus B_{i-1} \oplus B_i\oplus A_i=0\)).

Here, let us look at a basis of the sequence. If the basis is found by applying Gaussian elimination in the order \(i=1,2,\ldots\), no operation will change whether each element is contained in that basis. This property follows from the character of \(B\) in [2].

Additionally, if there is a basis vector in position \(j\), an operation with \(K=j+1\) can cause a change by \(B_j\) to \(B_1\oplus B_2 \oplus \ldots \oplus B_{i-1} \oplus B_i\) without causing changes by the basis vectors to the right of \(j\).

It follows that one can make \(A_i=B_i\) in \(\mathrm{O}(\log M)\) operations by taking the basis vectors from right to left, checking whether the operation for the current basis vector is required, and carrying it out if it is required.

Therefore, if the necessary and sufficient condition in [2] is satisfied, one can always match \(B\) to \(A\) in \(\mathrm{O}(N\log M)\) operations.

posted:
last update: