We have a two-dimensional array with the number 0 in the upper left corner. The rest of the array is then filled with numbers so that each index contains the smallest positive integer possible that already exists neither on the same row or column.
Example:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
1 0 3 2 5 4 7 6 9 8 11 10 13 12 15 14 17 16
2 3 0 1 6 7 4 5 10 11 8 9 14 15 12 13 18 19
3 2 1 0 7 6 5 4 11 10 9 8 15 14 13 12 19 18
4 5 6 7 0 1 2 3 12 13 14 15 8 9 10 11 20 21
5 4 7 6 1 0 3 2 13 12 15 14 9 8 11 10 21 20
6 7 4 5 2 3 0 1 14 15 12 13 10 11 8 9 22 23
7 6 5 4 3 2 1 0 15 14 13 12 11 10 9 8 23 22
8 9 10 11 12 13 14 15 0 1 2 3 4 5 6 7 24 25
9 8 11 10 13 12 15 14 1 0 3 2 5 4 7 6 25 24
10 11 8 9 14 15 12 13 2 3 0 1 6 7 4 5 26 27
11 10 9 8 15 14 13 12 3 2 1 0 7 6 5 4 27 26
12 13 14 15 8 9 10 11 4 5 6 7 0 1 2 3 28 29
13 12 15 14 9 8 11 10 5 4 7 6 1 0 3 2 29 28
14 15 12 13 10 11 8 9 6 7 4 5 2 3 0 1 30 31
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 31 30
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 0 1
17 16 19 18 21 20 23 22 25 24 27 26 29 28 31 30 1 0
Given the row and the column in such array, I need to be able to find the number in the specified index in less than one second on a relatively new desktop PC (for row and column less than a million). My brute-force attempts so far have been so futile that it’s clearly not the way I want to go with this. Presumably there must be a way to find out the number in question, in linear time (?), that doesn’t require computing all the preceding numbers in the array.
Observation shows that the operator is the bitwise
XOR(represent each operand as a binary number, XOR together the corresponding bits, read as binary).Now on to prove it is the XOR:
Since the XOR with one argument fixed is a bijection on the other argument, the “that exists neither on the same row or column” is satisfied.
Now it just suffices to prove the “smallest” part, namely that any lower value already occurs if we reduce either operand:
or equivalently
Note that we are no longer concerned about our operator, we’re proving the minimality of XOR.
Define
C = A xor Bif
A = 0,B = 0, then minimality is satisfied.Now, if
AandBhave the same magnitude (the same bit length), then clearing the top bit of both will not changeC. Clearing the top bit is a translation towards the origin in the matrix, so if a smaller value exists above or to the left after translation, it is at the same relative position before the translation.AandBmust have a different magnitude to be a counter-example.XOR(as well as the operator under consideration) are symmetric, so assumeA > B.If
Fis of greater magnitude thanA, then it’s not smaller, and thus it’s not a counter-example.If
Fhas the same magnitude asA, then clear the highest bit inAand inF. This is a translation in the table. It changes the values, but not their ordering, so if a smaller value exists above or to the left after translation, it is at the same relative position before the translation.If
Fhas a smaller magnitude thanA, then, by the pigeonhole principle and the properties of XOR, there exists aDwith a smaller magnitude thanAsuch thatD xor B = F.summary: The proof that XOR satisfies the conditions imposed onto the solution follows from the symmetries of XOR, its magnitude-preserving properties and its bijection properties. We can find each smaller element than
A xor Bby reducingA,Band the challenge until they’re all zero or of different magnitude (at which point we apply the pigeonhole principle to prove the challenge can be countered without actually countering it).