Source: IMO-2023-P5
Category: Extremal Problems

Problem: Let $n$ be a positive integer. A Japanese triangle consists of $1 + 2 + \cdots + n$ circles arranged in an equilateral triangular shape such that for each $1 \leq i \leq n$, the ithrow contains exactly $i$ circles, exactly one of which is colored red. A ninja path in a Japanese triangle is a sequence of $n$ circles obtained by starting in the top row, then repeatedly going from a circle to one of the two circles immediately below it and finishing in the bottom row. In terms of $n$, find the greatest $k$ such that in each Japanese triangle there is a ninja 
path containing at least $k$ red circles.

Answer: $k = \lfloor  \log_2(n) \rfloor  + 1$

Reference solution:
The answer is
\[
k = \lfloor \log_2(n) \rfloor + 1.
\]

**Construction (Upper Bound):**
It suffices to construct a Japanese triangle for any $n$ such that no ninja path contains more than $\lfloor \log_2(n) \rfloor + 1$ red circles. 
Let the circles be indexed by $(i, j)$, where $i$ is the row number ($1 \le i \le n$) and $j$ is the column number ($1 \le j \le i$). A ninja path moves from $(i, j)$ to either $(i+1, j)$ or $(i+1, j+1)$ at each step.
Partition the rows $1, 2, \dots, n$ into $e = \lfloor \log_2(n) \rfloor + 1$ blocks. For $1 \le m \le e$, the $m$-th block consists of rows $i$ such that $2^{m-1} \le i \le \min(n, 2^m - 1)$. 
For each row $i$ in the $m$-th block, we can write $i = 2^{m-1} + d$ for some integer $0 \le d < 2^{m-1}$. We place the red circle in row $i$ at column $2d + 1$. (This is a valid position because $2d + 1 \ge 1$ and $i - (2d + 1) = 2^{m-1} - d - 1 \ge 0$, so $2d + 1 \le i$.)

Suppose a ninja path passes through two red circles in the same block $m$, say at rows $i = 2^{m-1} + d$ and $i' = 2^{m-1} + d'$ with $d < d'$. The red circle at row $i$ is at column $2d+1$. At each step, a ninja path can increase its column index by at most $1$. Thus, moving from row $i$ to row $i'$, the column index can increase by at most $i' - i = d' - d$. Therefore, at row $i'$, the path can reach a column index of at most $(2d+1) + (d' - d) = d + d' + 1$. However, the red circle at row $i'$ is located at column $2d' + 1$. Since $d < d'$, we have $d + d' + 1 < 2d' + 1$, meaning the ninja path cannot reach the red circle at row $i'$.
This proves that any ninja path contains at most one red circle per block. Since there are $e$ blocks, any ninja path contains at most $e = \lfloor \log_2(n) \rfloor + 1$ red circles.

**Lower Bound (First Proof):**
Conversely, we show that in any Japanese triangle, one can find a ninja path containing at least $\lfloor \log_2(n) \rfloor + 1$ red circles. Let $(i, r_i)$ denote the unique red circle in row $i$.

We construct a directed graph $T_1$ on the set of all circles by defining the outgoing edges from row $i$ to row $i+1$ (for $1 \le i < n$) as follows:
- The red circle $(i, r_i)$ has two outgoing edges, connecting to both circles immediately below it: $(i+1, r_i)$ and $(i+1, r_i+1)$.
- Any white circle $(i, j)$ to the left of the red circle ($j < r_i$) has one outgoing edge, connecting to $(i+1, j)$.
- Any white circle $(i, j)$ to the right of the red circle ($j > r_i$) has one outgoing edge, connecting to $(i+1, j+1)$.

In row $i+1$, circles $(i+1, 1)$ through $(i+1, r_i-1)$ each receive exactly one incoming edge (from the white circles to the left of $r_i$). Circles $(i+1, r_i)$ and $(i+1, r_i+1)$ each receive exactly one incoming edge (from the red circle). Circles $(i+1, r_i+2)$ through $(i+1, i+1)$ each receive exactly one incoming edge (from the white circles to the right of $r_i$). Thus, every circle in row $i+1$ has exactly one incoming edge from row $i$. Since row 1 contains only the root circle $(1,1)$ (which must be red), $T_1$ is a rooted binary tree where every path from the root to a leaf in the bottom row is a valid ninja path.

Next, we build a new rooted tree $T_2$ consisting only of the $n$ red circles. We define the edges of $T_2$ by connecting each red circle to its closest red descendant(s) in $T_1$. That is, a red circle $v$ is a child of a red circle $u$ in $T_2$ if there is a directed path in $T_1$ from $u$ to $v$ containing only white circles as intermediate nodes.
In $T_1$, each red circle has exactly two children, and each white circle has exactly one child. Therefore, any path in $T_1$ starting from a child of a red circle $u$ does not branch until it reaches another red circle (or a leaf). This implies that $u$ can have at most two children in $T_2$.
Thus, $T_2$ is a binary tree with $n$ nodes. Let $k$ be the maximum number of nodes on a path from the root to a leaf in $T_2$. Since a binary tree of depth $k$ has at most $2^k - 1$ nodes, we must have $2^k - 1 \ge n$, which implies $k \ge \lfloor \log_2(n) \rfloor + 1$.
This path of length $k$ in $T_2$ corresponds to a sequence of $k$ red circles that lie on a single path in $T_1$. Since any path in $T_1$ is a valid ninja path, there exists a ninja path containing at least $\lfloor \log_2(n) \rfloor + 1$ red circles.

**Lower Bound (Alternative Recursive Proof):**
Define $f(i, j)$ as the maximum number of red circles contained in the portion of a ninja path from $(1, 1)$ down to $(i, j)$, including the endpoints. If $j < 1$ or $j > i$, we define $f(i, j) = 0$. Since every ninja path reaching $(i, j)$ must pass through either $(i-1, j-1)$ or $(i-1, j)$, we have the recurrence:
\[
f(i, j) = \max \{f(i-1, j-1), f(i-1, j)\} + \begin{cases} 1 & \text{if } (i, j) \text{ is red} \\ 0 & \text{otherwise} \end{cases}
\]
Define $S_i = \sum_{j=1}^i f(i, j)$.

**Claim 1:** $S_{i+1} \ge S_i + \left\lceil \frac{S_i}{i} \right\rceil + 1$.
*Proof.* We evaluate $S_{i+1}$:
\[
S_{i+1} = \sum_{j=1}^{i+1} \max \{f(i, j-1), f(i, j)\} + \sum_{j=1}^{i+1} \begin{cases} 1 & \text{if } (i+1, j) \text{ is red} \\ 0 & \text{otherwise} \end{cases}
\]
Since exactly one circle in row $i+1$ is red, the second sum is exactly $1$. Let $f(i, m)$ be the maximum element among $\{f(i, 1), \dots, f(i, i)\}$. We split the first sum at $m$:
\begin{align*}
S_{i+1} &= \sum_{j=1}^m \max \{f(i, j-1), f(i, j)\} + \sum_{j=m+1}^{i+1} \max \{f(i, j-1), f(i, j)\} + 1 \\
&\ge \sum_{j=1}^m f(i, j) + \sum_{j=m+1}^{i+1} f(i, j-1) + 1 \\
&= \sum_{j=1}^m f(i, j) + \sum_{j=m}^i f(i, j) + 1 \\
&= S_i + f(i, m) + 1
\end{align*}
Since $f(i, m)$ is the maximum of $i$ integers whose sum is $S_i$, by the Pigeonhole Principle, $f(i, m) \ge \lceil \frac{S_i}{i} \rceil$. Thus, $S_{i+1} \ge S_i + \lceil \frac{S_i}{i} \rceil + 1$. $\blacksquare$

**Claim 2:** Write $n = 2^c + r$, where $0 \le r < 2^c$. Then $S_n \ge cn + 2r + 1$.
*Proof.* We proceed by induction on $n$.
Base case $n=1$: We have $1 = 2^0 + 0$, so $c=0$ and $r=0$. The only circle in row $1$ is red, so $S_1 = f(1, 1) = 1$. The formula gives $0 \cdot 1 + 2(0) + 1 = 1$, so the claim holds for $n=1$.
Assuming the claim holds for some $n=j$, let $j = 2^c + r$. We have:
\begin{align*}
S_{j+1} &\ge S_j + \left\lceil \frac{S_j}{j} \right\rceil + 1 \\
&\ge cj + 2r + 1 + \left\lceil \frac{cj + 2r + 1}{j} \right\rceil + 1 \\
&= cj + 2r + 1 + c + \left\lceil \frac{2r + 1}{j} \right\rceil + 1
\end{align*}
Since $j = 2^c + r$ and $r \le 2^c - 1$, we have $j - (2r+1) = 2^c - r - 1 \ge 0$, which means $2r+1 \le j$. Thus, $0 < \frac{2r+1}{j} \le 1$, which implies $\lceil \frac{2r+1}{j} \rceil = 1$. Therefore:
\[
S_{j+1} \ge cj + 2r + 1 + c + 1 + 1 = c(j+1) + 2(r+1) + 1
\]
If $j+1$ is not a power of $2$, then $j+1 = 2^c + (r+1)$ with $r+1 < 2^c$. The formula matches exactly.
If $j+1$ is a power of $2$, then $r+1 = 2^c$, so $j+1 = 2^{c+1}$. We can rewrite the bound as:
\[
c(j+1) + 2(2^c) + 1 = c(j+1) + (j+1) + 1 = (c+1)(j+1) + 2(0) + 1
\]
This matches the formula for $j+1$ with parameters $c+1$ and $0$. The induction is complete. $\blacksquare$

Finally, we show that this implies the existence of a path with at least $c+1$ red circles. The average value of $f(n, j)$ for $1 \le j \le n$ is $\frac{S_n}{n}$. Thus, the maximum value is at least $\lceil \frac{S_n}{n} \rceil$. Using Claim 2:
\[
\left\lceil \frac{S_n}{n} \right\rceil \ge \left\lceil \frac{cn + 2r + 1}{n} \right\rceil = c + \left\lceil \frac{2r + 1}{n} \right\rceil = c + 1
\]
Therefore, there exists some $j$ such that $f(n, j) \ge c+1$. Since $c = \lfloor \log_2(n) \rfloor$, there is a ninja path containing at least $\lfloor \log_2(n) \rfloor + 1$ red circles.

Grading guidelines:
(Partial) 1. Gave the correct candidate value k = ⌊log2(n)⌋ + 1, with some justification from small cases or a heuristic (e.g. “at most one per power-of-2 block” or “binary tree depth”). 2. Proved a nontrivial upper bound by an explicit coloring construction showing that some Japanese triangle forces every ninja path to have O(log n) red circles (e.g. partitioning rows into blocks [2^{m-1},2^m−1] and arguing a path cannot hit two reds within one block). 3. Proved a nontrivial lower bound of the form k ≥ ⌊log2(n)⌋ (or k ≥ c+1 where 2^c ≤ n), e.g. by introducing the rooted/binary-tree encoding of paths or a dynamic-programming recurrence f(i,j), but without completing the final counting step. (Almost) 1. Proved the upper bound construction correctly (at most one red per block, hence ≤ ⌊log2(n)⌋+1), and proved the lower bound via the binary-tree argument, but left a minor gap in defining/justifying the tree T2 (e.g. why each red has ≤2 red-children, or why a path in T2 corresponds to a valid ninja path). 2. Proved the lower bound via the f(i,j), S_i method up to a correct inequality such as S_{i+1} ≥ S_i + ⌈S_i/i⌉ + 1 (or an equivalent growth estimate), but made a small induction/boundary slip when converting it into max_j f(n,j) ≥ ⌊log2(n)⌋+1. 3. Solution is almost complete, but made minor mistakes which are not negligible.

Construction task:
For $n=8$, provide an explicit construction of a Japanese triangle that minimizes the maximum number of red circles any ninja path can contain.

Requirements:
- Represent the triangle as an 8x8 matrix $M$, where $M[i][j]$ is the circle in row $i$, column $j$ (0-indexed).
- Use `0` for cells outside the triangle (where $j > i$).
- Use `1` for white circles and `2` for red circles.
- Each row $i \in \{0, \dots, 7\}$ must contain exactly one red circle (`2`) among its $i+1$ circles.
- A ninja path is a sequence of cells $(r, c_r)$ for $r=0, \dots, 7$ where $c_0=0$ and $c_{r+1} \in \{c_r, c_r+1\}$.
- Provide exactly one construction block and put ONLY the Python literal list of lists between the tags:
<construct>
[...matrix...]
</construct>
- Do not use code fences. Do not put any extra text inside <construct>...</construct>.

Reference construction:
[[2, 0, 0, 0, 0, 0, 0, 0], [2, 1, 0, 0, 0, 0, 0, 0], [1, 1, 2, 0, 0, 0, 0, 0], [2, 1, 1, 1, 0, 0, 0, 0], [1, 1, 2, 1, 1, 0, 0, 0], [1, 1, 1, 1, 2, 1, 0, 0], [1, 1, 1, 1, 1, 1, 2, 0], [2, 1, 1, 1, 1, 1, 1, 1]]

Verifier code:
import sys
import ast

def verify():
    try:
        # Read the entire raw input from stdin
        data = sys.stdin.read().strip()
        if not data:
            print("Error: No input provided.")
            return
        
        # Parse the input using a strict Python-literal strategy
        try:
            matrix = ast.literal_eval(data)
        except Exception as e:
            print(f"Error: Failed to parse input as a Python literal. {e}")
            return
        
        # 1. Structural Verification: Matrix must be 8x8
        if not isinstance(matrix, list) or len(matrix) != 8:
            print(f"Error: Matrix must be a list of 8 rows, but found {len(matrix) if isinstance(matrix, list) else 'non-list'}.")
            return
        
        for i in range(8):
            if not isinstance(matrix[i], list) or len(matrix[i]) != 8:
                print(f"Error: Row {i} must be a list of 8 elements.")
                return
        
        # 2. Constraint Verification: Lower triangular property and red circle constraint
        for i in range(8):
            red_count = 0
            for j in range(8):
                val = matrix[i][j]
                if j > i:
                    # Outside the triangle (j > i)
                    if val != 0:
                        print(f"Error: Cell ({i}, {j}) is outside the triangle (j > i) and must be 0.")
                        return
                else:
                    # Inside the triangle (j <= i)
                    if val == 2:
                        red_count += 1
                    elif val == 1:
                        pass
                    else:
                        print(f"Error: Cell ({i}, {j}) must be 1 (white) or 2 (red).")
                        return
            
            # Exactly one red circle per row
            if red_count != 1:
                print(f"Error: Row {i} must contain exactly one red circle (value 2), but found {red_count}.")
                return

        # 3. Mathematical Verification: Dynamic Programming to find the max red circles on any ninja path
        # dp[r][c] stores the maximum number of red circles on a ninja path starting at (0,0) and ending at (r,c)
        dp = [[-1] * 8 for _ in range(8)]
        
        # Initial condition (Row 0)
        # We already verified there is exactly one '2' in row 0, which must be at (0,0)
        dp[0][0] = (1 if matrix[0][0] == 2 else 0)
        
        for r in range(1, 8):
            for c in range(r + 1):
                # A ninja path can reach (r, c) from (r-1, c) or (r-1, c-1)
                prev_max = -1
                
                # Path from (r-1, c)
                if c < r:
                    if dp[r-1][c] != -1:
                        prev_max = max(prev_max, dp[r-1][c])
                
                # Path from (r-1, c-1)
                if c > 0:
                    if dp[r-1][c-1] != -1:
                        prev_max = max(prev_max, dp[r-1][c-1])
                
                # If reachable, calculate max red circles for current cell
                if prev_max != -1:
                    dp[r][c] = prev_max + (1 if matrix[r][c] == 2 else 0)
                else:
                    # Every cell in the triangle should be reachable under ninja path rules
                    print(f"Error: Cell ({r}, {c}) is unreachable, which violates triangle structure.")
                    return

        # The objective is the maximum value in the final row
        max_path_reds = max(dp[7])
        
        # The theoretical minimum of the maximum number of red circles for n=8 is floor(log2(8)) + 1 = 4.
        # The instruction asks for a construction that minimizes this maximum.
        if max_path_reds == 4:
            print("True")
        else:
            print(f"Error: The maximum number of red circles on any ninja path is {max_path_reds}, but it should be 4.")
            
    except Exception as e:
        print(f"Error: An unexpected error occurred during verification. {e}")

if __name__ == "__main__":
    verify()