Codeforces Round 956 (Div. 2) and ByteRace 2024
B
Corner Twist
Codeforces
1200
原题
← AC →
翻译中…

You are given two grids of numbers $a$ and $b$, with $n$ rows and $m$ columns. All the values in the grid are $0$, $1$ or $2$.

翻译中…

You can perform the following operation on $a$ any number of times:

翻译中…
  • Pick any subrectangle in the grid with length and width $\ge 2$. You are allowed to choose the entire grid as a subrectangle.
  • The subrectangle has four corners. Take any pair of diagonally opposite corners of the chosen subrectangle and add $1$ to their values modulo $3$.
  • For the pair of corners not picked, add $2$ to their values modulo $3$.
翻译中…

Note that the operation only changes the values of the corners of the picked subrectangle.

翻译中…

Is it possible to convert the grid $a$ into grid $b$ by applying the above operation any number of times (possibly zero)?

翻译中…

Input

翻译中…

The first line contains an integer $t$, the number of testcases ($1 \le t \le 250$).

翻译中…

For each testcase:

翻译中…

The first line contains two integers $n$ and $m$, the number of rows and columns in the grid ($2 \le n,m \le 500$).

翻译中…

Each of the next n lines contain m characters — the $j$-th character of the $i$-th line represents $a_{i,j}$.

翻译中…

Each of the next n lines contain m characters — the $j$-th character of the $i$-th line represents $b_{i,j}$ ($0 \le a_{i,j}, b_{i,j} \le 2$).

翻译中…

It is guaranteed that the sum of $n$ over all test cases and the sum of $m$ over all test cases do not exceed $500$.

翻译中…

Output

翻译中…

For each test case print "YES" (without quotes) if it is possible to convert grid $a$ into grid $b$ and "NO" (without quotes) otherwise.

翻译中…

You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.

翻译中…

Note

翻译中…

In the first testcase, grid $a$ can be converted into $b$ in the following manner:

翻译中…

$\begin{matrix}\fbox{0} & 0 & \fbox{0}\\ 0 & 0 & 0\\ \fbox{0} & 0 & \fbox{0}\end{matrix} \Rightarrow \begin{matrix}1 & 0 & 2\\ 0 & \fbox{0} & \fbox{0}\\ 2 & \fbox{0} & \fbox{1}\end{matrix} \Rightarrow \begin{matrix}1 & 0 & 2\\ \fbox{0} & \fbox{1} & 2\\ \fbox{2} & \fbox{2} & 2\end{matrix} \Rightarrow \begin{matrix}1 & \fbox{0} & \fbox{2}\\ 1 & 0 & 2\\ 1 & \fbox{0} & \fbox{2}\end{matrix} \Rightarrow \begin{matrix}1 & 1 & 1\\ 1 & \fbox{0} & \fbox{2}\\ 1 & \fbox{2} & \fbox{0}\end{matrix} \Rightarrow \begin{matrix}1 & 1 & 1\\ 1 & 1 & 1\\ 1 & 1 & 1\end{matrix}$

翻译中…

Here, in each operation, the top-right and bottom-left corners highlighted by a box are incremented by $2$ modulo $3$, while the top-left and bottom-right corners are incremented by $1$ modulo $3$.

翻译中…

In the fourth testcase, it can be proven that it is not possible to convert grid $a$ into grid $b$ using the above-mentioned operations any number of times.

翻译中…

样例

Input #1
7
3 3
000
000
000
111
111
111
4 4
0000
0000
0000
0000
2100
1200
0012
0021
4 4
1020
1200
1210
0000
0000
1200
2200
0000
3 3
012
012
012
010
111
011
8 8
00000000
00000000
00000000
00000000
00000000
00000000
00000000
10000000
00000000
01200000
02010000
00102000
00020100
00001020
00000210
10000000
2 7
0000000
0000000
2220111
0111222
2 7
0000000
0100010
2220111
1210202
Output #1
YES
YES
YES
NO
YES
NO
YES
InputCopy OutputCopy
题目信息
题号
1983B
时间限制
1 s
内存限制
256 MB
标签
构造
贪心
模拟
数学

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。