DMOPC '22 Contest 4 P2 - Fake Painting

View as PDF

Submit solution


Points: 10 (partial)
Time limit: 2.0s
Memory limit: 256M

Author:
Problem type

In the 20th century, Picasso used an unconventional painting technique to create his art. Initially, he started with a canvas A that can be represented by an N by M grid of integers. Let (x,y) denote the position of the cell in the x-th row from the top and y-th column from the left.

Picasso painted the canvas using a special type of brushstroke, which he used an unknown (possibly zero) number of times. Each brushstroke consisted of the following: First, he chose a nonzero integer K and a position (x,y). Then, he added K to cell (x,y), flipped the grid either horizontally or vertically, and added K to cell (x,y) again.

Specifically, a horizontal flip reverses the order of the columns, while a vertical flip reverses the order of the rows.

You want to buy one of Picasso's masterpieces, however, it could be a fake. Given the original canvas A and a potential canvas T, determine if T could have been created by Picasso.

Constraints

1N,M1500

1Ai,j109

1Ti,j109

Subtask 1 [30%]

N=2

M=2

Subtask 2 [70%]

No additional constraints.

Input Specification

The first line contains 2 space-separated integers N and M.

The next N lines contain M space-separated integers Ai,j representing the cells of the original canvas.

The last N lines contain M space-separated integers Ti,j representing the cells of the potential canvas.

Output Specification

Output YES if T could have been created by Picasso, and NO otherwise.

Sample Input 1

Copy
2 2
1 2
3 5
2 1
4 2

Sample Output 1

Copy
YES

Explanation for Sample 1

Picasso can perform 1 brushstroke to transform A into T: choose K as 1, add K to A2,1, flip the grid horizontally, and add K to A2,1 again.

Sample Input 2

Copy
2 2
1 2
3 5
1 2
3 6

Sample Output 2

Copy
NO

Explanation for Sample 2

No sequence of moves can be performed which transforms A to T.

Sample Input 3

Copy
1 3
2 3 2
1 2 2

Sample Output 3

Copy
NO

Explanation for Sample 3

No sequence of moves can be performed which transforms A to T.


Comments

There are no comments at the moment.