#P7515. Matrix Restoration
Matrix Restoration
Matrix Restoration
Alice has an \(n \times m\) matrix \(a_{i,j}\) (with \(1 \le i \le n\) and \(1 \le j \le m\)) consisting of non-negative integers not exceeding \(10^6\). Bob computed a matrix \(b\) of size \((n-1) \times (m-1)\) from \(a\) via the formula:
\[ b_{i,j} = a_{i,j} + a_{i,j+1} + a_{i+1,j} + a_{i+1,j+1} \]
for all \(1 \le i \le n-1\) and \(1 \le j \le m-1\).
Now, Alice has forgotten the original matrix \(a\). Given the matrix \(b\), restore a valid matrix \(a\) (with non-negative integers) that could have generated \(b\) using the formula above. Note that the original matrix \(a\) is guaranteed to exist and satisfy \(0 \le a_{i,j} \le 10^6\). One valid method is to set the first row and first column of \(a\) to 0, and then for every \(1 \le i \le n-1\) and \(1 \le j \le m-1\) compute: \[ a_{i+1,j+1} = b_{i,j} - a_{i,j} - a_{i,j+1} - a_{i+1,j}. \] Indices in the above formulas are 1-based. (For implementation use 0-based indexing appropriately.)
inputFormat
The first line contains two integers \(n\) and \(m\) (\(n, m \ge 2\)). The next \(n-1\) lines each contain \(m-1\) integers. The \(j\)-th number in the \(i\)-th of these lines is \(b_{i,j}\).
outputFormat
Output the restored matrix \(a\) in \(n\) lines. Each line should contain \(m\) integers separated by spaces. It is guaranteed that there exists at least one solution.
sample
2 2
5
0 0
0 5
</p>