Source: CMO-2012-P2
Category: Counting

Problem: Given a prime number $p$. Let $A = (a_{ij})$ be a $p \\times p$ matrix such that $\\{a_{ij} \\mid 1 \\leq i, j \\leq p\\} = \\{1, 2, \\cdots, p^2\\}$. The following operation is allowed: choose a row or a column, and simultaneously add $1$ or subtract $1$ to each number in that row or column. If it is possible to turn all elements of $A$ into $0$ after finitely many such operations, then $A$ is called a \"good matrix\". Find the number of \"good matrices\".


Answer: $2(p!)^2$

Reference solution:
Let the operations applied to the rows and columns be represented by subtracting $x_i$ from row $i$ and $y_j$ from column $j$. The matrix can be turned into the zero matrix if and only if the original entries satisfy $a_{ij} = x_i + y_j$ for some integers $x_i$ and $y_j$ for all $1 \le i, j \le p$. 

Since the entries of the matrix are a permutation of $\{1, 2, \dots, p^2\}$, we have 
\[
\{x_i + y_j \mid 1 \le i, j \le p\} = \{1, 2, \dots, p^2\}.
\]
Let us define the polynomials 
\[
f(z) = \sum_{i=1}^{p} z^{x_i} \quad \text{and} \quad g(z) = \sum_{j=1}^{p} z^{y_j}.
\]
Then, the condition on the sums $x_i + y_j$ translates to the polynomial identity
\[
f(z)g(z) = z + z^2 + \cdots + z^{p^2}.
\]
We can factor the right-hand side as
\[
z + z^2 + \cdots + z^{p^2} = z(1 + z + \cdots + z^{p-1})(1 + z^p + \cdots + z^{p(p-1)}).
\]
Because the cyclotomic polynomials $\Phi_p(z) = 1 + z + \cdots + z^{p-1}$ and $\Phi_{p^2}(z) = 1 + z^p + \cdots + z^{p(p-1)}$ are both irreducible, and $f(1) = g(1) = p$, each of $f(z)$ and $g(z)$ must be the product of a monomial $z^a$ and exactly one of $\Phi_p(z)$ or $\Phi_{p^2}(z)$. Consequently, the set $\{x_1, x_2, \dots, x_p\}$ must be one of the following:
\[
\{0, 1, \dots, p-1\} \quad \text{or} \quad \{1, p+1, \dots, p(p-1)+1\} \quad \text{or} \quad \{1, 2, \dots, p\} \quad \text{or} \quad \{0, p, \dots, p(p-1)\}.
\]
From this, it is easy to obtain the solution to the original problem, yielding a total of $2(p!)^2$ good matrices.

Grading guidelines:
(Partial) 1. Reformulated the operations: showed that $A$ is good iff there exist integers $(x_i)_{i=1}^p,(y_j)_{j=1}^p$ with $a_{ij}=x_i+y_j$ for all $i,j$ (equivalently, all $2\times2$ minors satisfy $a_{ij}+a_{kl}=a_{il}+a_{kj}$). 2. Set up the generating-function identity $f(z)=\sum_{i=1}^p z^{x_i},\ g(z)=\sum_{j=1}^p z^{y_j}$ and derived $f(z)g(z)=z+z^2+\cdots+z^{p^2}$ from the multiset condition $\{x_i+y_j\}=\{1,2,\dots,p^2\}$. 3. From the identity (or from modular/spacing considerations), correctly obtained at least one of the two structural possibilities: either $\{x_i\}$ is a translate of $\{0,1,\dots,p-1\}$ and $\{y_j\}$ is a translate of $\{0,p,2p,\dots,(p-1)p\}$, or the swapped roles of rows/columns. (Almost) 1. Completed the classification of possible exponent sets (up to translation): proved that $\{x_i\}$ and $\{y_j\}$ must correspond to the two cyclotomic factors (hence to one of the two “arithmetic progression mod $p$ / progression step $p$” patterns), but did not finish converting this into the final count $2(p!)^2$. 2. Counted the matrices once the two patterns for $(x_i),(y_j)$ are identified, but missed a global shift/normalization issue (e.g. double-counted or forgot the symmetry swapping rows and columns), leading to an answer off by a factor of $2$ or by a constant factor. 3. Solution is almost complete, but made minor mistakes which are not negligible.

Construction task:
For the specific case $p=3$, provide an explicit construction of all valid "good matrices".

Construction requirements:
- Output a single Python list containing all such matrices.
- Each matrix must be represented as a list of 3 lists, where each inner list represents a row of 3 integers.
- The matrices can be listed in any order, but there must be no duplicates.
- Your output must be exhaustive, including every possible good matrix exactly once.
- Each matrix must contain all integers from 1 to 9 exactly once.
- Each matrix must be a "good matrix" according to the problem definition (i.e., it can be reduced to the zero matrix by the allowed operations).
- Put ONLY the Python literal list of lists of lists inside the construction block.

Reference construction:
[
  [[1, 2, 3], [4, 5, 6], [7, 8, 9]],
  [[1, 3, 2], [4, 6, 5], [7, 9, 8]],
  [[2, 1, 3], [5, 4, 6], [8, 7, 9]],
  [[2, 3, 1], [5, 6, 4], [8, 9, 7]],
  [[3, 1, 2], [6, 4, 5], [9, 7, 8]],
  [[3, 2, 1], [6, 5, 4], [9, 8, 7]],
  [[1, 2, 3], [7, 8, 9], [4, 5, 6]],
  [[1, 3, 2], [7, 9, 8], [4, 6, 5]],
  [[2, 1, 3], [8, 7, 9], [5, 4, 6]],
  [[2, 3, 1], [8, 9, 7], [5, 6, 4]],
  [[3, 1, 2], [9, 7, 8], [6, 4, 5]],
  [[3, 2, 1], [9, 8, 7], [6, 5, 4]],
  [[4, 5, 6], [1, 2, 3], [7, 8, 9]],
  [[4, 6, 5], [1, 3, 2], [7, 9, 8]],
  [[5, 4, 6], [2, 1, 3], [8, 7, 9]],
  [[5, 6, 4], [2, 3, 1], [8, 9, 7]],
  [[6, 4, 5], [3, 1, 2], [9, 7, 8]],
  [[6, 5, 4], [3, 2, 1], [9, 8, 7]],
  [[4, 5, 6], [7, 8, 9], [1, 2, 3]],
  [[4, 6, 5], [7, 9, 8], [1, 3, 2]],
  [[5, 4, 6], [8, 7, 9], [2, 1, 3]],
  [[5, 6, 4], [8, 9, 7], [2, 3, 1]],
  [[6, 4, 5], [9, 7, 8], [3, 1, 2]],
  [[6, 5, 4], [9, 8, 7], [3, 2, 1]],
  [[7, 8, 9], [1, 2, 3], [4, 5, 6]],
  [[7, 9, 8], [1, 3, 2], [4, 6, 5]],
  [[8, 7, 9], [2, 1, 3], [5, 4, 6]],
  [[8, 9, 7], [2, 3, 1], [5, 6, 4]],
  [[9, 7, 8], [3, 1, 2], [6, 4, 5]],
  [[9, 8, 7], [3, 2, 1], [6, 5, 4]],
  [[7, 8, 9], [4, 5, 6], [1, 2, 3]],
  [[7, 9, 8], [4, 6, 5], [1, 3, 2]],
  [[8, 7, 9], [5, 4, 6], [2, 1, 3]],
  [[8, 9, 7], [5, 6, 4], [2, 3, 1]],
  [[9, 7, 8], [6, 4, 5], [3, 1, 2]],
  [[9, 8, 7], [6, 5, 4], [3, 2, 1]],
  [[1, 4, 7], [2, 5, 8], [3, 6, 9]],
  [[1, 4, 7], [3, 6, 9], [2, 5, 8]],
  [[2, 5, 8], [1, 4, 7], [3, 6, 9]],
  [[2, 5, 8], [3, 6, 9], [1, 4, 7]],
  [[3, 6, 9], [1, 4, 7], [2, 5, 8]],
  [[3, 6, 9], [2, 5, 8], [1, 4, 7]],
  [[1, 7, 4], [2, 8, 5], [3, 9, 6]],
  [[1, 7, 4], [3, 9, 6], [2, 8, 5]],
  [[2, 8, 5], [1, 7, 4], [3, 9, 6]],
  [[2, 8, 5], [3, 9, 6], [1, 7, 4]],
  [[3, 9, 6], [1, 7, 4], [2, 8, 5]],
  [[3, 9, 6], [2, 8, 5], [1, 7, 4]],
  [[4, 1, 7], [5, 2, 8], [6, 3, 9]],
  [[4, 1, 7], [6, 3, 9], [5, 2, 8]],
  [[5, 2, 8], [4, 1, 7], [6, 3, 9]],
  [[5, 2, 8], [6, 3, 9], [4, 1, 7]],
  [[6, 3, 9], [4, 1, 7], [5, 2, 8]],
  [[6, 3, 9], [5, 2, 8], [4, 1, 7]],
  [[4, 7, 1], [5, 8, 2], [6, 9, 3]],
  [[4, 7, 1], [6, 9, 3], [5, 8, 2]],
  [[5, 8, 2], [4, 7, 1], [6, 9, 3]],
  [[5, 8, 2], [6, 9, 3], [4, 7, 1]],
  [[6, 9, 3], [4, 7, 1], [5, 8, 2]],
  [[6, 9, 3], [5, 8, 2], [4, 7, 1]],
  [[7, 1, 4], [8, 2, 5], [9, 3, 6]],
  [[7, 1, 4], [9, 3, 6], [8, 2, 5]],
  [[8, 2, 5], [7, 1, 4], [9, 3, 6]],
  [[8, 2, 5], [9, 3, 6], [7, 1, 4]],
  [[9, 3, 6], [7, 1, 4], [8, 2, 5]],
  [[9, 3, 6], [8, 2, 5], [7, 1, 4]],
  [[7, 4, 1], [8, 5, 2], [9, 6, 3]],
  [[7, 4, 1], [9, 6, 3], [8, 5, 2]],
  [[8, 5, 2], [7, 4, 1], [9, 6, 3]],
  [[8, 5, 2], [9, 6, 3], [7, 4, 1]],
  [[9, 6, 3], [7, 4, 1], [8, 5, 2]],
  [[9, 6, 3], [8, 5, 2], [7, 4, 1]]
]

Verifier code:
import sys
import ast

def verify():
    payload = sys.stdin.read().strip()
    if not payload:
        print("Empty payload.")
        return
    
    try:
        matrices = ast.literal_eval(payload)
    except Exception as e:
        print(f"Failed to parse payload as Python literal: {e}")
        return
        
    if not isinstance(matrices, list):
        print("The payload must be a list.")
        return
        
    if len(matrices) != 72:
        print(f"Expected exactly 72 matrices, but got {len(matrices)}.")
        return
        
    unique_matrices = set()
    
    for idx, matrix in enumerate(matrices):
        if not isinstance(matrix, list) or len(matrix) != 3:
            print(f"Matrix at index {idx} is not a list of exactly 3 elements.")
            return
            
        elements = []
        tuple_matrix = []
        for r_idx, row in enumerate(matrix):
            if not isinstance(row, list) or len(row) != 3:
                print(f"Row {r_idx} of matrix at index {idx} is not a list of exactly 3 elements.")
                return
            for val in row:
                if type(val) is not int:
                    print(f"Element {val} in matrix {idx} is not an integer.")
                    return
                elements.append(val)
            tuple_matrix.append(tuple(row))
            
        unique_matrices.add(tuple(tuple_matrix))
        
        if sorted(elements) != [1, 2, 3, 4, 5, 6, 7, 8, 9]:
            print(f"Matrix at index {idx} does not contain exactly the integers 1 to 9.")
            return
            
        for i in range(3):
            for j in range(3):
                if matrix[i][j] - matrix[i][0] != matrix[0][j] - matrix[0][0]:
                    print(f"Matrix at index {idx} is not a 'good matrix'. Failed property at i={i}, j={j}.")
                    return
                    
    if len(unique_matrices) != 72:
        print(f"Expected 72 unique matrices, but found {len(unique_matrices)}.")
        return
        
    print("True")

if __name__ == "__main__":
    verify()