Throughout the solution we denote the cells of the table by coordinate pairs; (i,j) refers to the jth cell in the ith row.
Consider the directed graph, whose vertices are the cells of the board, and the edges are the arrows (i,j)→(j,i+j) for all 0≤i,j≤2n−1. From each vertex (i,j), exactly one edge passes (to (j,i+jmod2n)); conversely, to each cell (j,k) exactly one edge is directed (from the cell (k−jmod2n,j)). Hence, the graph splits into cycles.
Now, in any coloring considered, the vertices of each cycle should have the same color by the problem condition. On the other hand, if each cycle has its own color, the obtained coloring obviously satisfies the problem conditions. Thus, the maximal possible number of colors is the same as the number of cycles, and we have to prove that this number is 2n.
Next, consider any cycle (i1,j1),(i2,j2),…; we will describe it in other terms. Define a sequence (a0,a1,…) by the relations a0=i1,a1=j1,an+1=an+an−1 for all n≥1 (we say that such a sequence is a Fibonacci-type sequence). Then an obvious induction shows that ik≡ak−1(mod2n),jk≡ak(mod2n). Hence we need to investigate the behavior of Fibonacci-type sequences modulo 2n.
Denote by F0,F1,… the Fibonacci numbers defined by F0=0,F1=1, and Fn+2=Fn+1+Fn for n≥0. We also set F−1=1 according to the recurrence relation.
For every positive integer m, denote by ν(m) the exponent of 2 in the prime factorization of m, i.e. for which 2ν(m)∣m but 2ν(m)+1∤m.
Lemma 1. For every Fibonacci-type sequence a0,a1,a2,…, and every k≥0, we have ak=Fk−1a0+Fka1.
*Proof.* Apply induction on k. The base cases k=0,1 are trivial. For the step, from the induction hypothesis we get
ak+1=ak+ak−1=(Fk−1a0+Fka1)+(Fk−2a0+Fk−1a1)=Fka0+Fk+1a1.
Lemma 2. For every m≥3,
(a) we have ν(F3⋅2m−2)=m;
(b) d=3⋅2m−2 is the least positive index for which 2m∣Fd;
(c) F3⋅2m−2+1≡1+2m−1(mod2m).
*Proof.* Apply induction on m. In the base case m=3 we have ν(F3⋅2m−2)=F6=8, so ν(F3⋅2m−2)=ν(8)=3, the preceding Fibonacci-numbers are not divisible by 8, and indeed F3⋅2m−2+1=F7=13≡1+4(mod8).
Now suppose that m>3 and let k=3⋅2m−3. By applying Lemma 1 to the Fibonacci-type sequence Fk,Fk+1,… we get
F2k=Fk−1Fk+FkFk+1=(Fk+1−Fk)Fk+Fk+1Fk=2Fk+1Fk−Fk2F2k+1=Fk2+Fk+12
By the induction hypothesis, ν(Fk)=m−1, and Fk+1 is odd. Therefore we get ν(Fk2)=2(m−1)>(m−1)+1=ν(2FkFk+1), which implies ν(F2k)=m, establishing statement (a).
Moreover, since Fk+1=1+2m−2+a2m−1 for some integer a, we get
F2k+1=Fk2+Fk+12≡0+(1+2m−2+a2m−1)2≡1+2m−1(mod2m)
as desired in statement (c).
We are left to prove that 2m∤Fℓ for ℓ<2k. Assume the contrary. Since 2m−1∣Fℓ, from the induction hypothesis it follows that ℓ>k. But then we have Fℓ=Fk−1Fℓ−k+FkFℓ−k+1, where the second summand is divisible by 2m−1 but the first one is not (since Fk−1 is odd and ℓ−k<k). Hence the sum is not divisible even by 2m−1. A contradiction.
Now, for every pair of integers (a,b)=(0,0), let μ(a,b)=min{ν(a),ν(b)}. By an obvious induction, for every Fibonacci-type sequence A=(a0,a1,…) we have μ(a0,a1)=μ(a1,a2)=…; denote this common value by μ(A). Also denote by pn(A) the period of this sequence modulo 2n, that is, the least p>0 such that ak+p≡ak(mod2n) for all k≥0.
Lemma 3. Let A=(a0,a1,…) be a Fibonacci-type sequence such that μ(A)=k<n. Then pn(A)=3⋅2n−1−k.
*Proof.* First, we note that the sequence (a0,a1,…) has period p modulo 2n if and only if the sequence (a0/2k,a1/2k,…) has period p modulo 2n−k. Hence, passing to this sequence we can assume that k=0.
We prove the statement by induction on n. It is easy to see that for n=1,2 the claim is true; actually, each Fibonacci-type sequence A with μ(A)=0 behaves as 0,1,1,0,1,1,… modulo 2, and as 0,1,1,2,3,1,0,1,1,2,3,1,… modulo 4 (all pairs of residues from which at least one is odd appear as a pair of consecutive terms in this sequence).
Now suppose that n≥3 and consider an arbitrary Fibonacci-type sequence A=(a0,a1,…) with μ(A)=0. Obviously we should have pn−1(A)∣pn(A), or, using the induction hypothesis, s=3⋅2n−2∣pn(A). Next, we may suppose that a0 is even; hence a1 is odd, and a0=2b0, a1=2b1+1 for some integers b0,b1.
Consider the Fibonacci-type sequence B=(b0,b1,…) starting with (b0,b1). Since a0=2b0+F0,a1=2b1+F1, by an easy induction we get ak=2bk+Fk for all k≥0. By the induction hypothesis, we have pn−1(B)∣s, hence the sequence (2b0,2b1,…) is s-periodic modulo 2n. On the other hand, by Lemma 2 we have Fs+1≡1+2n−1(mod2n),F2s≡0(mod2n),F2s+1≡1(mod2n), hence
as+1=2bs+1+Fs+1≡2b1+1+2n−1≡2b1+1=a1(mod2n)a2s=2b2s+F2s≡2b0+0=a0(mod2n)a2s+1=2b2s+1+F2s+1≡2b1+1=a1(mod2n)
The first line means that A is not s-periodic, while the other two provide that a2s≡a0, a2s+1≡a1 and hence a2s+t≡at for all t≥0. Hence s∣pn(A)∣2s and pn(A)=s, which means that pn(A)=2s, as desired.
Finally, Lemma 3 provides a straightforward method of counting the number of cycles. Actually, take any number 0≤k≤n−1 and consider all the cells (i,j) with μ(i,j)=k. The total number of such cells is 22(n−k)−22(n−k−1)=3⋅22n−2k−2. On the other hand, they are split into cycles, and by Lemma 3 the length of each cycle is 3⋅2n−1−k. Hence the number of cycles consisting of these cells is exactly 3⋅2n−1−k3⋅22n−2k−2=2n−k−1. Finally, there is only one cell (0,0) which is not mentioned in the previous computation, and it forms a separate cycle. So the total number of cycles is
1+k=0∑n−12n−1−k=1+(1+2+4+⋯+2n−1)=2n