#P10644. Constructing a Matrix from Absolute Differences
Constructing a Matrix from Absolute Differences
Constructing a Matrix from Absolute Differences
In this problem, we are given a grid of size representing a city. Each cell has an unknown electricity consumption value , which can be positive, negative, or zero. For each cell, we define [ C_{i,j} = \left|\sum_{k=1}^N A_{k,j} - \sum_{k=1}^M A_{i,k}\right|. ]
In other words, for every cell the absolute difference between the total consumption of its column and the total consumption of its row is given. You are provided with all values of and need to determine a valid construction of the matrix . It is guaranteed that there is at least one solution.
A hint on our approach: define the row sums as and the column sums as . Then for every we have [ |Y_j - X_i| = C_{i,j}. ] We can choose and set for . For each , cell gives [ |C_{1,1} - X_i| = C_{i,1}, ] which implies two possible candidates for : either or . Choose the candidate that satisfies (|C_{1,j}-X_i|=C_{i,j}) for all .
Once and are determined (note that they satisfy (\sum_{i=1}^N X_i = \sum_{j=1}^M Y_j)), a valid matrix can be constructed as follows:
For the first row, set [ A_{1,1} = X_1 - \sum_{j=2}^M Y_j, \quad A_{1,j} = Y_j \quad (j\ge2). ] For each row , set [ A_{i,1} = X_i, \quad A_{i,j} = 0 \quad (j\ge2). ]
This construction guarantees that the row sums of are and the column sums are , thereby satisfying the conditions.
Your task is to implement this solution. The input starts with two integers and , followed by the grid . The output should be the constructed matrix , printed as lines with space‐separated integers on each line.
inputFormat
The first line contains two integers and . Then follow lines, each containing integers where the -th integer in the -th line is .
outputFormat
Output lines, each containing space-separated integers representing the matrix that satisfies the given conditions.
sample
2 2
4 3
3 4
-3 3
7 0
</p>