#P3208. [HNOI2010] 矩阵
[HNOI2010] 矩阵
Description
Xiao Z has recently been studying matrices. He first wrote an matrix where each cell contains a non-negative integer less than . Then, for every submatrix, he computed the sum of its four numbers.
For example, when , the matrix he wrote is:
$$A = \begin{pmatrix} 0 & 1 & 2 \\ 1 & 2 & 0 \\ 2 & 0 & 1 \end{pmatrix}$$There are submatrices of size , and their sums are:
$$A = \begin{pmatrix} 0 & 0 & 0 \\ 0 & 4 & 5 \\ 0 & 5 & 3 \end{pmatrix}$$(The zeros in the first row and first column are added only for formatting.)
Now Xiao Z wants to see whether he can reconstruct the original matrix from these sums. Since his math is not very good, he asks you to do it.
Of course, the solution may not be unique. For example, the following matrix produces the same sums as above:
$$A = \begin{pmatrix} 0 & 2 & 1 \\ 0 & 2 & 0 \\ 2 & 1 & 0 \end{pmatrix}$$If multiple matrices satisfy the requirement, output the lexicographically smallest one.
Lexicographic order is defined as follows: for two solution matrices and , find the cell with the smallest row index where they differ; if there are multiple such cells, take the one with the smallest column index. The matrix with the smaller value at that cell is lexicographically smaller.
For the two example matrices above, the first differing cell is the second cell in the first row, and since the first matrix has a smaller value there, it is lexicographically smaller.
Also, it is guaranteed that the input is solvable.
Input Format
The first line contains three positive integers , denoting the number of rows and columns of the matrix and the value range of each number. Then follow lines, each containing non-negative integers. In the -th line, the -th number is the sum of the submatrix whose bottom-right corner is cell . The numbers in the first row and the first column are guaranteed to be , and each sum does not exceed .
Output Format
Output lines, each containing integers describing the matrix you reconstructed, with adjacent integers separated by single spaces. Do not print trailing spaces at the end of a line.
3 3 3
0 0 0
0 4 5
0 5 3
0 0 2
2 2 1
1 0 0
Hint
.
.
Thanks to @ASC_8384 for providing the problem statement.
Translated by ChatGPT 5
京公网安备 11011102002149号