#K83437. Matrix Transformation
Matrix Transformation
Matrix Transformation
You are given an n × m matrix. In one move, you can add 1 to all elements of any row or any column. Your task is to determine if it is possible to make all the elements in the matrix equal by applying these operations any number of times.
Formally, given a matrix \( A = [a_{ij}] \) where \( 1 \le i \le n \) and \( 1 \le j \le m \), you can perform the following operation repeatedly: \[ \text{Choose any row or column and add } 1 \text{ to every element in that row or column.} \]
You need to decide whether there exists a sequence of such moves that transforms \( A \) into a matrix where all entries are equal.
Note: It is allowed to perform zero operations if the matrix already has all equal elements.
The mathematical condition for the transformation can be expressed in \( \LaTeX \) as follows: For every two rows \( i \) and \( k \), the differences \( [a_{ij} - a_{1j}]_{j=1}^m \) must be identical (up to a constant difference) and similarly for the columns.
inputFormat
The input is read from standard input (stdin) and has the following format:
n m row1_element1 row1_element2 ... row1_element_m row2_element1 row2_element2 ... row2_element_m ... rown_element1 rown_element2 ... rown_element_m
Where n and m are the dimensions of the matrix.
outputFormat
Output a single line to standard output (stdout) containing either YES
if it is possible to make all elements equal using the allowed operations, or NO
otherwise.
2 2
1 2
3 4
YES