#P6497. Matrix Average Integer Problem

    ID: 19711 Type: Default 1000ms 256MiB

Matrix Average Integer Problem

Matrix Average Integer Problem

Slavko wants to fill an \(n\times n\) matrix with \(n^2\) distinct positive integers such that:

  • The average of the \(n\) numbers in each row is an integer that appears somewhere in that row. In other words, for every row \(i\) with elements \(a_{i,1},a_{i,2},\dots,a_{i,n}\) we require \[ \frac{a_{i,1}+a_{i,2}+\cdots+a_{i,n}}{n} = x_i \quad \text{for some} \quad x_i \in \{a_{i,1},a_{i,2},\dots,a_{i,n}\}. \]
  • The average of the \(n\) numbers in each column is an integer that appears somewhere in that column. That is, for every column \(j\) with elements \(a_{1,j},a_{2,j},\dots,a_{n,j}\) we have \[ \frac{a_{1,j}+a_{2,j}+\cdots+a_{n,j}}{n} = y_j \quad \text{for some} \quad y_j \in \{a_{1,j},a_{2,j},\dots,a_{n,j}\}. \]
  • Every element satisfies \(1 \le a_{i,j} \le 10^9\) and all \(n^2\) numbers are distinct.

Your task is to help Slavko by providing any valid construction of such a matrix.

Note: It can be shown that when n is odd, one valid solution is to fill the matrix in row-major order with consecutive integers. In each row (being an arithmetic progression with an odd number of terms) the average equals the middle element. Similarly, each column (also an arithmetic progression with an odd number of terms) has its average equal to its middle element. For this problem, you may assume that the input n is an odd integer and \(n \ge 3\).

inputFormat

The input consists of a single line containing an odd integer n (with \(n \ge 3\)).

For example:

3

outputFormat

Output the constructed \(n \times n\) matrix. Each of the next n lines should contain n space-separated positive integers representing one row of the matrix.

For example, a valid output for the sample input 3 is:

1 2 3
4 5 6
7 8 9

sample

3
1 2 3

4 5 6 7 8 9

</p>