Official

B - Unique XOR Path Editorial by evima


The values in the squares in the path represented by \(S\) can all be \(0\). (If the value in square \((i,j)\) is \(v\), \(XOR\) the values in all squares \((r,c)\) such that \(r+c=i+j\) by \(v\).)

Whenever the path goes RD, one can change it to DR to visit one extra square. The same goes for DR.

Consider picking up from \(S\) as many substrings that are RD or DR as possible. Here, those substrings should not intersect.

This can be achieved by just picking them up greedily from the beginning. Assume that \(A\) substrings are picked up in this way. Then, there will be \(A\) squares not in the path represented by \(S\) such that one can visit any subset of those squares.

For instance, consider \(S=\)RRDRDDRD.

000..
.#00.
..#0#
...00
....0

Here, # shows the extra squares corresponding to the three strings that can be picked up as \(S=\)R[RD][RD][DR]D.

Clearly, \(A \leq K\) is necessary. This is also sufficient.

Let us try writing different powers of \(2\) at the corners corresponding to the RDs and DRs. For the other corners, let us write the same value as the previous corner. Furthermore, extend those values at the corners diagonally.

An example with \(S=\)RDRDRD:

001.
1002
.200
2.40

An example with \(S=\)RRDRDDRD:

0001.
.100.
1.204
.2.00
2..40

(The values of the . squares are \(0\). They are shown as . for visibility.)

Then, any path other than the one represented by \(S\) will visit a non-empty subset of the \(A\) powers of \(2\), whose total \(XOR\) is non-zero, satisfying the condition.

Sample solution

posted:
last update: