#K64277. Matrix Transformation Challenge
Matrix Transformation Challenge
Matrix Transformation Challenge
Frodo is given two matrices A and B of dimensions \(n \times m\). He can perform the following two sets of operations in order on matrix A in an attempt to transform it into matrix B:
- Column Scaling: For each column \(j\) (\(1 \leq j \leq m\)), if an element \(a_{ij}\) is nonzero, Frodo can multiply all nonzero entries in that column by a constant factor \(k_j\). For every nonzero element in the column, the relation must be consistent, i.e., if \(a_{ij} \neq 0\), then \(b_{ij}\) must be an integer multiple of \(a_{ij}\), and the quotient must be the same for all nonzero entries in that column.
- Row Shifting: For each row \(i\) (\(1 \leq i \leq n\)), Frodo can add a constant offset \(d_i\) to every element in that row.
Your task is to determine whether it is possible to transform matrix A into matrix B by applying these operations in the specified order. Output YES
if the transformation is possible, or NO
otherwise.
The operations can be summarized with the following constraints:
- For each column \(j\), if \(a_{ij} \neq 0\) then there must exist a factor \(k_j\) such that \(b_{ij} = k_j \cdot a_{ij}\) for all applicable \(i\).
- For each row \(i\), there must exist an offset \(d_i\) such that \(a'_{ij} + d_i = b_{ij}\) for all \(j\), where \(a'_{ij}\) is the updated value after the column scaling.
inputFormat
The input begins with a single integer \(T\) denoting the number of test cases.
Each test case is described as follows:
- A line containing two integers \(n\) and \(m\), representing the number of rows and columns respectively.
- Next \(n\) lines where each line contains \(m\) integers, representing the matrix A.
- Following that, another \(n\) lines where each line contains \(m\) integers, representing the matrix B.
outputFormat
For each test case, output a single line containing either YES
if matrix B can be obtained from matrix A by applying the allowed operations, or NO
otherwise.
2
2 2
1 2
3 4
2 4
6 8
1 3
1 2 3
1 2 4
YES
NO
</p>