Official

E - Not Equal Rectangle Editorial by evima


Below, the matrix is \(0\)-indexed. Also, we use integers between \(0\) and \(24\) (and add \(1\) to everything in the end).

In short

Let \(P = 23\). Write \((\lfloor \frac{i}{P} \rfloor \lfloor\frac{j}{P} \rfloor + i + j )\bmod P\) at the \(i\)-th row and \(j\)-th column, and the given condition will be entirely satisfied.

Sample Implementation (Python):

n, m = map(int, input().split())
p = 23
for i in range(n):
    print(*[((i // p) * (j // p) + i + j) % p + 1 for j in range(m)])

Analysis

Consider a more general problem: for any prime number \(P\), paint a \(P^2 \times P^2\) grid with integers between \(0\) and \(P-1\) to satisfy the condition.

If we solve this, we can let \(P=23\), construct a grid for \(P^2 = 529 \times 529\), and then print the top-left part with \(N\) rows and \(M\) columns.

We call a \(P\times P\) grid a small block. We will make \(P\) kinds of small blocks \(B_0,B_1,\ldots,B_{P-1}\) and arrange \(P\times P\) small blocks accordingly to construct a \(P^2\times P^2\) grid.

For the small grid \(B_k\), let the \((i,j)\) entry be \((i + j + k) \bmod P\).

For example, for \(P=3\), \(B\) will be:

\[ B_0 = \begin{pmatrix} 0 & 1 & 2\\ 1& 2 & 0 \\ 2& 0 & 1 \\ \end{pmatrix}, B_1 = \begin{pmatrix} 1& 2 & 0 \\ 2& 0 & 1 \\ 0 & 1 & 2\\ \end{pmatrix}, B_2 = \begin{pmatrix} 2& 0 & 1 \\ 0 & 1 & 2\\ 1& 2 & 0 \\ \end{pmatrix}. \]

Now let us place \(P\times P\) of these small blocks. We put \(B_{ij \bmod P}\) at the position \((i,j)\).

For example, for \(P=3\), we get:

\[ \begin{pmatrix} B_0& B_0 & B_0 \\ B_0& B_1 & B_2\\ B_0& B_2 & B_1 \\ \end{pmatrix} = \begin{pmatrix} 0 & 1 & 2& 0 & 1 & 2 & 0 & 1 & 2\\ 1& 2 & 0 & 1 & 2 & 0 & 1 & 2& 0\\ 2 & 0 & 1 & 2 & 0 & 1 & 2& 0\ & 1\\ 0 & 1 & 2& 1 & 2 & 0 & 2& 0\ & 1\\ 1& 2 & 0 & 2 & 0 & 1 & 0 & 1 & 2\\ 2 & 0 & 1 &0 & 1 & 2 & 1 & 2& 0\\ 0 & 1 & 2& 2& 0\ & 1 & 1 & 2 & 0\\ 1& 2 & 0& 0 & 1 & 2 & 2& 0\ & 1\\ 2 & 0 & 1 & 1 & 2 & 0 & 0 & 1 & 2\\ \end{pmatrix}. \]

Let us prove that this construction satisfies the condition.

Since the numbers in each row and each column of a small block are distinct, the condition is satisfied when \((x_1,x_2,y_1,y_2)\) lies within a small block or straddles two small blocks. Now, all we have to check is that the condition is satisfied when \((x_1,x_2,y_1,y_2)\) straddles four small blocks.

Let \(a,b,c,d\) be the indices of the four small blocks straddled by \((x_1,x_2,y_1,y_2)\), in clockwise order starting at the top left. If all four entries were equal, we would have \(a-d = b-c\) from the way \(B\) are constructed, which should not happen because we put \(B_{ij \bmod P}\) at the position \((i,j)\).

This completes the proof.

posted:
last update: