#!/bin/sh set -eu PROOF=${PROOF:-/app/proof.md} mkdir -p "$(dirname -- "$PROOF")" cat > "$PROOF" <<'IMO_PROOF_cea0c2df075ed8246adc37d8bf574440c966b69f8b36db62bb793248d83659b7' Let $(i, j)$ denote the cell in the $i$-th column from the left and the $j$-th row from the bottom. For any $n \geq 5$, we can color the grid with $q=3$ and $r:= n^2 - kq \leq n+\lceil 2 \sqrt{n}\rceil+2$ so that it does not satisfy the conditions. From this, we can see that $a(n) \geq \frac{n^{2}- (n+2 \sqrt{n}+3)}{3}$. (1) Let $m=\lceil\sqrt{n}\rceil-1$. (2) For the following cells, color the $2 \times 2$ square with that cell as its bottom left corner with the same color. \[ \{(i, j): i, j \in \mathbb{Z}, m|i, \quad m| j, \quad 1 \leq i, j \leq n-1\} \] Color each $2 \times 2$ square with a different color. Let $A$ be the area colored in this way. (3) In the remaining area excluding $A$, we can color the cells so that each color is used 3 times. For any $i$ such that $m \mid i$, color the cells $(x, y)$ with $x=i$ that do not belong to $A$ with the same color as the cell above it, and color the cells $(x, y)$ with $y=i$ that do not belong to $A$ with the same color as the cell to its right. Color the remaining cells arbitrarily, but make sure that each color appears 3 or 4 times. (4) Since we can do this with $k=\left\lfloor\frac{n^{2}-|A|}{3}\right\rfloor, r \leq|A|+2$. (5) From this, \[ |A|=\left\lfloor\frac{n-1}{m}\right\rfloor^{2}<\left(\frac{n-1}{\sqrt{n}-1}\right)^{2} If $k \geq \frac{n^{2}}{3}$, the conditions are satisfied. Therefore, $a(n) \leq\left\lceil\frac{n^{2}}{3}\right\rceil \leq \frac{n^{2}+2}{3}$. (1) Consider the following graph: each cell is a vertex, and each cell is connected to the vertices of cells adjacent to it that have a different color. (2) Consider the $4 n-4$ cells on the border of the given $n \times n$ grid. Now, let's prove the following claim. Claim. For any two vertices $x, y$ belonging to the same connected component $\Phi$ on the border, if there is no vertex in $\Phi$ when moving clockwise along the border from $x$ to $y$, then the vertex one step clockwise from $x$ and the vertex one step counterclockwise from $y$ belong to the same connected component. (3) Let $z$ be the vertex one step clockwise from $x$ and $w$ be the vertex one step counterclockwise from $y$. Consider the boundary of the polygon formed by the cells in $\Phi$. This boundary consists of the perimeter edges of the given $n \times n$ grid or the edges shared by two adjacent cells within the $n \times n$ grid. The edge $l$ shared by $x$ and $z$ and the edge $m$ shared by $y$ and $w$ are both included in the boundary of this polygon. By the condition, on the boundary of this polygon, the line segments between $l$ and $m$ are all inside the $n \times n$ grid. (4) Let these line segments between $l$ and $m$ be $a_{0}, a_{1}, \cdots, a_{k}$ in order ( $a_{0}=l, a_{k}=m$ ). Each $a_{i}$ lies on the boundary between a cell belonging to $\Phi$ and a cell not belonging to $\Phi$. That is, the two cells sharing $a_{i}$ must have the same color. We will show that the cells not belonging to $\Phi$ that share $a_{i}$ all belong to the same connected component. We will use induction to show that for $i=0, \cdots, s$, the cells not belonging to $\Phi$ that share $a_{i}$ all belong to the same connected component. (5) Now, if $s=0$, it is trivial since there is only one cell. Therefore, using the inductive hypothesis, assume that the statement holds for $s-1$ and consider the case for $s$. (Case 1) If $a_{s-1}$ and $a_{s}$ lie on the same line This is the case where for some $2 \times 2$ square and the line connecting the midpoints of two opposite sides, one side belongs to $\Phi$ and the opposite side does not. In this case, the coloring must be symmetric with respect to this line. Therefore, the two cells not belonging to $\Phi$ must also be connected by a line segment. (Case 2) If $a_{s-1}$ and $a_{s}$ form a $90^{\circ}$ angle This means that for some $2 \times 2$ square, the two lines connecting its center to the midpoints of two consecutive sides are $a_{s-1}$ and $a_{s}$. These lines divide the $2 \times 2$ square into 1 cell and 3 cells. Place the centers of these cells on a new coordinate plane. Suppose they are divided into $(0,0)$ and $(1,0),(0,1),(1,1)$. Then $(0,0)$ and $(1,0),(0,1)$ must have the same color. Since each color is used at most 3 times, these are different from the color of $(1,1)$. Therefore, $(1,1)$ is connected to $(1,0)$ and $(0,1)$. That is, both sides of the division belong to the same connected component. (6) Therefore, by mathematical induction, all cells not belonging to $\Phi$ that share $a_{i}$ are in the same connected component. This means that $z$ and $w$ are in the same connected component. This completes the proof of the Claim. (7) Returning to the main proof, we will consider the vertices on the border. For two points $(x_1, y_1)$ and $(x_2, y_2)$, define the 'grid distance' as $|x_1 -x_2| +|y_1 - y_2|$. Let points $a$ and $b$ be the pair of points on the boarder that have the largest grid distance between them while being on the same connected component. (8) If the grid distance between $a$ and $b$ is larger or equal to $n-1$, then the path on the connected component that connects $a$ and $b$ is a desired \textit{snake} and we are done. (9) If the grid distance is smaller than $n-1$, then let the squares one step clockwise from $a$ and $b$ be $a_1$ and $b_1$, respectively, and the squares one step counterclockwise from $a$ and $b$ be $a_2$ and $b_2$, respectively. Now without loss of generality, we can say the grid distance between $a_1$ and $b_2$ is smaller than $n-1$. Now think of the path along the boundary connecting $a_2$ and $b_1$, not passing $a$ and $b$. The length of this path is at least $4n-4 -(n-1)-2 = 3n-5$, so if there is a square $c$ that is in the same connected component with $a$ and $b$, the grid distance between $(a,c)$ or $(b,c)$ is larger than $n-1$, thus a contradiction to maximality. However, if there are no squares in the path from $a_2$ to $b_1$ , by Claim $a_2$ and $b_1$ must be in the same connected component, which is agian a contradiction to the maximality of $(a,b)$. IMO_PROOF_cea0c2df075ed8246adc37d8bf574440c966b69f8b36db62bb793248d83659b7