Editorial
The values and are never changed by any operation. Hence and are necessary.
Define the adjacent XOR array by for . If an operation is performed at position , only changes, so only and are affected. A direct calculation shows that these two values are swapped, and all other values remain unchanged.
Therefore one operation is exactly an adjacent swap in the adjacent XOR array. Since arbitrary permutations can be obtained by adjacent swaps, the adjacent XOR array can be reordered arbitrarily.
Conversely, once is fixed, the adjacent XOR array uniquely determines the whole array. Thus can be made equal to if and only if the following conditions hold.
- .
- .
- The multiset of for equals the multiset of for .
For each test case, build the two adjacent XOR arrays, sort them, and compare them. The time complexity is per test case, and the memory complexity is .
Solution written by GPT5.5