思路
显然,不管如何操作,每行、每列之和除以 的余数始终不变。所以,我们只要枚举每行、每列,看看和除以 的余数是否相同就行。
代码
# include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
int t, n, m;
string a[505], b[505];
int main () {
ios::sync_with_stdio (0);
cin.tie (0);
cout.tie (0);
cin >> t;
while (t --) {
cin >> n >> m;
for (int i = 0; i < n; ++ i)
cin >> a[i];
for (int i = 0; i < n; ++ i)
cin >> b[i];
for (int i = 0, s1, s2; i < n; ++ i) {
s1 = s2 = 0;
for (int j = 0; j < m; ++ j)
s1 += a[i][j] - '0', s2 += b[i][j] - '0';
if (s1 % 3 != s2 % 3)
goto no;
}
for (int j = 0, s1, s2; j < m; ++ j) {
s1 = s2 = 0;
for (int i = 0; i < n; ++ i)
s1 += a[i][j] - '0', s2 += b[i][j] - '0';
if (s1 % 3 != s2 % 3)
goto no;
}
cout << "Yes\n";
continue ;
no:
cout << "No\n";
}
return 0;
}