#P11122. Table Transformation to Achieve Target Sum
Table Transformation to Achieve Target Sum
Table Transformation to Achieve Target Sum
You are given an n × m table of integers and a target sum s. Let \( S = \sum_{i=1}^{n}\sum_{j=1}^{m} a_{ij} \) be the initial sum of the table. In one operation, you can perform one of the following:
- Choose any row and add 1 to every element in that row (this increases the total sum by m).
- Choose any column and add 1 to every element in that column (this increases the total sum by n).
Your task is to determine if it is possible to obtain a table whose total sum is exactly s by performing a sequence of such operations. If it is possible, output "YES", followed by the number of operations and then a list of operations. Each operation should be output on a separate line in one of the following formats:
row x
(to indicate adding 1 to every element in row x, 1-indexed)col y
(to indicate adding 1 to every element in column y, 1-indexed)
If it is not possible, output a single line "NO".
The condition to check is whether there exist nonnegative integers x and y satisfying the equation:
\( S + m\,x + n\,y = s \)
If s − S is negative or cannot be expressed as a nonnegative linear combination of m and n, then the answer is "NO". Otherwise, output any valid sequence of operations (for example, you may perform x row operations on row 1 and y column operations on column 1).
inputFormat
The first line of input contains three integers n, m and s separated by spaces.
The following n lines each contain m integers representing the table's rows.
outputFormat
If it is possible to achieve the target sum s, output:
- A line with "YES".
- A line with an integer k representing the total number of operations.
- k lines each describing an operation in the format either
row x
orcol y
.
Otherwise, output a single line containing "NO".
sample
2 3 21
1 2 3
4 5 6
YES
0
</p>