Source: ELMO-2014-P6
Category: Extremal Problems

Problem: A $2^{2014} + 1$ by $2^{2014} + 1$ grid has some black squares filled. The filled black squares form one or more snakes on the plane, each of whose heads splits at some points but never comes back together. In other words, for every positive integer $n$ greater than $2$, there do not exist pairwise distinct black squares $s_1, s_2, \dots, s_n$ such that $s_i$ and $s_{i+1}$ share an edge for $i = 1, 2, \dots, n$ (here $s_{n+1} = s_1$).What is the maximum possible number of filled black squares?

Answer: $\frac{2(2^{2014}+1)(2^{2014}+2)}{3}-1$

Reference solution:
Let $n = 2^m + 1$ be the dimension of the grid (for the given problem, $m = 2014$). Let $v$ be the number of filled black squares, and $e$ be the number of shared edges between black squares.

Since the graph formed by the black squares contains no cycles, it is a forest, which implies $e \le v - 1$.
Double-counting the edges of the black squares yields $3v + 1 \le 4v - e$.
The total number of edges in the $n \times n$ grid is $2n(n+1)$. Since $4v - e$ is the number of grid edges covered by at least one black square, we have $4v - e \le 2n(n+1)$.
Thus, $3v + 1 \le 2n(n+1)$.

Because $n = 2^m + 1$, for any $m \ge 1$ we have $n \not\equiv 1 \pmod 3$, which means $n(n+1)$ is a multiple of $3$.
Therefore, $2n(n+1)$ is a multiple of $3$, and $3v + 1 \le 2n(n+1)$ implies $3v \le 2n(n+1) - 3$, yielding
$$v \le \frac{2n(n+1)}{3} - 1.$$
Observe that for $n \not\equiv 1 \pmod 3$, this equality is achieved if and only if:
(a) the graph formed by the black squares is a connected forest (i.e., a tree), and
(b) all but two square edges belong to at least one black square.

We prove by induction on $m \ge 1$ that equality can in fact be achieved.
For $m = 1$, take an “H-shape” on a $3 \times 3$ grid (so if we set the center at $(0,0)$ in the coordinate plane, everything but $(0,\pm 1)$ is black); call this configuration $G_1$. It is easy to verify that $G_1$ satisfies both conditions.

To go from $G_m$ to $G_{m+1}$, assume $G_m$ is defined on $[-2^{m-1}, 2^{m-1}]^2$. We define $G_{m+1}$ on $[-2^m, 2^m]^2$ as follows:
fill in $(2x,2y)$ in $G_{m+1}$ if and only if $(x,y)$ is filled in $G_m$, and fill in $(x,y)$ with $x,y$ not both even if and only if $x + y$ is odd (so exactly one of $x,y$ is odd and the other is even).

Each “newly-created” white square has both coordinates odd. Its four neighbors each have one even and one odd coordinate, so their coordinate sum is odd, meaning they are newly-created black squares. Thus, all four edges of any newly-created white square are covered by black squares.
The boundary of $G_{m+1}$ consists of squares with at least one coordinate equal to $\pm 2^m$ (which is even), so no newly-created white square lies on the boundary.
Furthermore, no two white squares can share an edge in $G_{m+1}$, because adjacent squares have different coordinate sum parities, and no square with an odd coordinate sum is white.
Therefore, the only uncovered edges in $G_{m+1}$ are the boundary edges of the “old” white squares that lie on the boundary of the grid. By the inductive hypothesis, there are exactly two such uncovered edges (originating from the two white squares $(0, \pm 1)$ in $G_1$). Thus $G_{m+1}$ satisfies (b).

To check that (a) holds, first we show that $(2x_1,2y_1)$ and $(2x_2,2y_2)$ are connected in $G_{m+1}$ if and only if $(x_1,y_1)$ and $(x_2,y_2)$ are black squares (and thus connected) in $G_m$ (the new black squares are essentially just “bridges”).
Indeed, every path of black squares in $G_{m+1}$ must alternate between squares with odd and even coordinate sums, which corresponds to alternating between new and old black squares. Two old black squares $(2x_1,2y_1)$ and $(2x_2,2y_2)$ are adjacent to the same new black square $(x_1 + x_2, y_1 + y_2)$ in $G_{m+1}$ if and only if $(x_1,y_1)$ and $(x_2,y_2)$ are adjacent in $G_m$, whence the claim readily follows.

The rest is clear: the set of old black squares must remain connected in $G_{m+1}$, and all new black squares (including those on the boundary) border at least one old black square (otherwise, its two even-sum neighbors would both be white, meaning two white squares would be adjacent in $G_m$, which violates condition (b) for $G_m$). Thus, $G_{m+1}$ is fully connected.

On the other hand, $G_{m+1}$ cannot have any cycles. Since each new black square has at most two old black neighbors (its other two neighbors have both coordinates odd and are thus white), any cycle in $G_{m+1}$ would alternate between new and old black squares. Removing the new black squares from such a cycle would yield a corresponding cycle of old black squares in $G_m$, which contradicts the inductive hypothesis.

Thus, the maximum possible number of filled black squares is achieved. For the given problem where $n = 2^{2014} + 1$, the maximum number of filled black squares is $\frac{2n(n+1)}{3} - 1$.

Grading guidelines:
(Partial) 1. Interpreted the filled squares as a graph (vertices = black squares, edges = shared sides) and observed that the “no cycle” condition implies the graph is a forest, hence e ≤ v−1. 2. Double-counted square-sides to obtain an inequality of the form 3v+1 ≤ 4v−e (or an equivalent bound relating v and e via boundary/perimeter counting). 3. Combined the side-count with the grid-edge bound 4v−e ≤ 2n(n+1) to deduce 3v+1 ≤ 2n(n+1), hence v ≤ 2n(n+1)/3 − 1 after the divisibility observation for n=2^m+1. (Almost) 1. Derived the sharp upper bound v ≤ 2n(n+1)/3 − 1 and correctly identified the equality conditions (connected forest and exactly two uncovered grid-edges), but did not provide a full construction achieving equality. 2. Gave the recursive/inductive construction (from a 3×3 base pattern to size 2^m+1) intended to achieve equality, but left a localized gap in verifying either (i) no cycles are created, or (ii) only two edges remain uncovered (e.g. boundary case). 3. Provided a complete construction and matching-count argument for general n=2^m+1 but made a small arithmetic/parity slip that affects only the final −1 or a boundary case, while the main method is correct.

Construction task:
Provide a concrete construction for the 17x17 grid (the $N = 2^4+1$ case) that achieves the maximum possible number of filled black squares without forming any cycles.

Your output must be a single, valid Python expression. When evaluated, this expression must return a 17x17 matrix (a list of lists) of strings, where "1" represents a filled black square and "0" represents an empty square. The orthogonally adjacent "1"s must form a forest (zero cycles), and the total count of "1"s must exactly equal the theoretical maximum for this 17x17 instance.

Do not state the specific numerical maximum. Do not use hardcoded full-grid enumeration; use compact Python constructs like list comprehensions and conditional expressions. Do not include any imports, assignments, function definitions, markdown formatting, or explanatory text. Put ONLY the Python expression inside the `<construct>` block.

Reference construction:
[["0" if (i%2==1 and j%2==1) or (i%4==2 and j%4==2) or (i%8==4 and j%8==4) or (i%16==0 and j==8) else "1" for j in range(17)] for i in range(17)]

Verifier code:
import sys

def verify():
    payload = sys.stdin.read().strip()
    if not payload:
        print("Empty payload")
        return

    try:
        grid = eval(payload, {"__builtins__": {}, "range": range}, {})
    except Exception as e:
        print(f"Parse error: {e}")
        return

    if not isinstance(grid, list) or len(grid) != 17:
        print("Grid must be a list of exactly 17 elements.")
        return

    for row in grid:
        if not isinstance(row, list) or len(row) != 17:
            print("Each row must be a list of exactly 17 elements.")
            return
        for cell in row:
            if not isinstance(cell, str) or cell not in ["0", "1"]:
                print("Each cell must be the string '0' or '1'.")
                return

    count_ones = sum(cell == "1" for row in grid for cell in row)
    if count_ones != 203:
        print(f"Total number of '1's must be exactly 203, but got {count_ones}.")
        return

    visited = set()
    for i in range(17):
        for j in range(17):
            if grid[i][j] == "1" and (i, j) not in visited:
                stack = [(i, j, -1, -1)]
                while stack:
                    curr_r, curr_c, parent_r, parent_c = stack.pop()
                    if (curr_r, curr_c) in visited:
                        print("Cycle detected among the '1's.")
                        return
                    visited.add((curr_r, curr_c))
                    
                    for dr, dc in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
                        nr, nc = curr_r + dr, curr_c + dc
                        if 0 <= nr < 17 and 0 <= nc < 17 and grid[nr][nc] == "1":
                            if nr == parent_r and nc == parent_c:
                                continue
                            stack.append((nr, nc, curr_r, curr_c))

    print("True")

if __name__ == "__main__":
    verify()