Source: CSMO-2019-P12
Category: Existence and Construction

Problem: In a $5\\times5$ matrix $X$, each element is either $0$ or $1$. Consider all $5$-tuples (ordered arrays) from the rows, columns, and diagonals of $X$ (a total of $24$): $(x_{i,1},x_{i,2},\\cdots,x_{i,5})$, $(x_{i,5},x_{i,4},\\cdots,x_{i,1})$ for $i=1,2,\\cdots,5$; $(x_{1,j},x_{2,j},\\cdots,x_{5,j})$, $(x_{5,j},x_{4,j},\\cdots,x_{1,j})$ for $j=1,2,\\cdots,5$; $(x_{1,1},x_{2,2},\\cdots,x_{5,5})$, $(x_{5,5},x_{4,4},\\cdots,x_{1,1})$, $(x_{1,5},x_{2,4},\\cdots,x_{5,1})$, $(x_{5,1},x_{4,2},\\cdots,x_{1,5})$. If all these arrays are pairwise distinct, find the possible values of the sum of all elements in matrix $X$.

Answer: 12 or 13

Reference solution:
Let the set of all $24$ five-tuples in the problem be denoted by $T$. Call a five-tuple $(a,b,c,d,e)$ "palindromic" if $a=e$ and $b=d$. Note that for each tuple $\alpha=(a_{1},a_{2},\dots,a_{5})\in T$, its reverse $\beta=(a_{5},a_{4},\dots,a_{1})$ is also in $T$. By the problem condition, all tuples in $T$ are distinct, so $\alpha\neq\beta$, which means $\alpha$ is not palindromic. Because the number of five-tuples with entries $0$ or $1$ that are non-palindromic is exactly $2^{5}-2^{3}=24$, $T$ is precisely the set of all non-palindromic five-tuples with entries $0$ or $1$.

For $\alpha\in T$, denote by $S_{\alpha}$ the sum of the five components of $\alpha$, and consider the total sum $\displaystyle\sum_{\alpha\in T} S_{\alpha}$.

On the one hand, pairing each $\alpha=(a_{1},a_{2},\dots,a_{5})\in T$ with its bitwise complement $\alpha'=(1-a_{1},1-a_{2},\dots,1-a_{5})$ shows that $\alpha'\in T$ and $S_{\alpha}+S_{\alpha'}=5$. Since the third components of $\alpha$ and $\alpha'$ differ, $\alpha\neq\alpha'$, meaning no $\alpha$ is paired with itself. Thus, the $24$ tuples in $T$ can be partitioned into $12$ pairs, giving $\displaystyle\sum_{\alpha\in T} S_{\alpha} = 12 \times 5 = 60$.

On the other hand, in the sum $\displaystyle\sum_{\alpha\in T} S_{\alpha}$, the central element $x_{3,3}$ of $X$ is counted $8$ times, each off-center element on the two main diagonals is counted $6$ times, and each element not on the diagonals is counted $4$ times. Let $S$ be the sum of all elements of $X$. Then we can express the total sum as:
\[
4x_{3,3}+2\bigl(x_{1,1}+x_{2,2}+x_{4,4}+x_{5,5}+x_{1,5}+x_{2,4}+x_{4,2}+x_{5,1}\bigr)+4S=60.
\]
From this equation and the constraints on the non-palindromic tuples, it follows that the possible values for the sum $S$ of all elements in matrix $X$ are $12$ and $13$.

Grading guidelines:
(Partial) 1. Observed that for each listed 5-tuple its reverse also appears in the list, hence every occurring 5-tuple must be non-palindromic (i.e. not equal to its reverse). 2. Counted the number of non-palindromic binary 5-tuples as 24, and concluded that the 24 arrays must be exactly the set of all non-palindromic binary 5-tuples. 3. Correctly set up a double-counting equation for \(\sum_{\alpha\in T} S_\alpha\) by counting how many times each entry \(x_{i,j}\) appears among the 24 arrays (center 8 times, other diagonal entries 6 times, off-diagonal entries 4 times), leading to a linear relation between this total and \(S=\sum x_{i,j}\). 4. Computed \(\sum_{\alpha\in T} S_\alpha=60\) (e.g. by pairing each tuple with its bitwise complement, or by another correct global symmetry argument). (Almost) 1. Derived the correct global equation (total sum over tuples equals 60, and also equals the weighted count of matrix entries) and reduced the problem to a small finite check on the diagonal/center variables, but did not complete the final casework needed to conclude \(S\in\{12,13\}\). 2. Correctly concluded that the 24 tuples are exactly all non-palindromic binary 5-tuples and that \(\sum_{\alpha\in T} S_\alpha=60\), and obtained a near-final formula giving \(S\) up to an unresolved parity/boundary issue (e.g. mishandled the contribution of \(x_{3,3}\) or one diagonal cell), so the final set of possible values for \(S\) is not fully justified. 3. Gave a complete derivation that forces \(S=12\) or \(S=13\), but provided only one explicit construction (or omitted verifying that both values are attainable under the distinctness condition). 4. Solution is almost complete, but made minor mistakes which are not negligible.

Construction task:
For every possible valid sum of all elements in the matrix, you must provide exactly one example of a $5 \times 5$ matrix that satisfies all the conditions.

Construction requirements:
- Output a single Python list containing lists of strings.
- Each inner list represents one $5 \times 5$ matrix and must contain exactly 5 strings of length 5, consisting only of the characters '0' and '1'.
- You must include exactly one matrix for each possible sum.
- For each matrix, the 24 tuples extracted (rows, columns, and diagonals, in both forward and reverse directions) must be pairwise distinct.
- Put ONLY the Python literal inside the construction block. Do not include any other text.

Reference construction:
[["10000", "11110", "11001", "00101", "11100"], ["01111", "00001", "00110", "11010", "00011"]]

Verifier code:
import ast
import sys

def verify():
    try:
        payload = sys.stdin.read().strip()
        if not payload:
            print("Empty input.")
            return
        data = ast.literal_eval(payload)
    except Exception as e:
        print(f"Parse error: {e}")
        return

    if not isinstance(data, list):
        print("Output must be a Python list.")
        return
    
    if len(data) != 2:
        print(f"Expected exactly 2 matrices, got {len(data)}.")
        return

    sums = set()

    for idx, M in enumerate(data):
        if not isinstance(M, list):
            print(f"Matrix at index {idx} must be a list.")
            return
        if len(M) != 5:
            print(f"Matrix at index {idx} must contain exactly 5 strings.")
            return
        
        total_sum = 0
        for row_idx, row in enumerate(M):
            if not isinstance(row, str):
                print(f"Row {row_idx} in matrix {idx} must be a string.")
                return
            if len(row) != 5:
                print(f"Row {row_idx} in matrix {idx} must be of length 5.")
                return
            if not all(c in '01' for c in row):
                print(f"Row {row_idx} in matrix {idx} must contain only '0' and '1'.")
                return
            total_sum += row.count('1')
            
        sums.add(total_sum)
        
        # Extract the 12 base tuples
        rows = [tuple(M[i]) for i in range(5)]
        cols = [tuple(M[i][j] for i in range(5)) for j in range(5)]
        diag1 = [tuple(M[i][i] for i in range(5))]
        diag2 = [tuple(M[i][4-i] for i in range(5))]
        
        base_tuples = rows + cols + diag1 + diag2
        
        # Generate the reversed counterparts, resulting in 24 tuples
        all_tuples = []
        for t in base_tuples:
            all_tuples.append(t)
            all_tuples.append(t[::-1])
            
        if len(set(all_tuples)) != 24:
            print(f"Matrix at index {idx} does not have 24 pairwise distinct tuples.")
            return

    if sums != {12, 13}:
        print(f"The set of sums must be exactly {{12, 13}}, got {sums}.")
        return

    print("True")

if __name__ == "__main__":
    verify()