Niton & Matrix 题解
注意到原操作等价于将关于矩阵中轴线对称的两个格子同时取反,在中轴线上的格子任意取值。
证明:
取反第
于是中轴线上的格子也是可以取反的,将整行取反后再将其它位置取反即可。
使用该操作也可以实现原操作。取反显然可以实现,翻转时若
原操作与该操作可以相互推导,因此等价。
于是矩阵中的格子构成了若干个自环和四元环,每条边上的两个格子可以同时取反。
自环显然不需考虑。容易发现四元环中的格子可以在不改变 1 个数的奇偶性的情况下任意调整,因此判断 1 个数奇偶性是否相同即可。
#include <bits/stdc++.h>
#define IOS std::ios::sync_with_stdio(false), std::cin.tie(nullptr), std::cout.tie(nullptr)
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
#define lcb(x) ((x) * size - size + 1)
#define rcb(x) ((x) * size)
#define lc(x) x << 1ll
#define rc(x) x << 1ll | 1ll
#define lowbit(x) ((x) & (-x))
#define LEN_LC (tree[lc(i)].r - tree[lc(i)].l + 1)
#define LEN_RC (tree[rc(i)].r - tree[rc(i)].l + 1)
#define LEN (tree[i].r - tree[i].l + 1)
#define int long long
#define ull unsigned long long
#define endl "\n"
const int N = 1e6 + 7;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
template<typename T>inline void read(T &x) {
bool f = 1;
x = 0;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = !f;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
x = (f ? x : -x);
return;
}
int n, m;
int calc(int i, int j) {
return i * m + j;
}
void solve() {
std::cin >> n >> m;
std::vector<std::vector<int >> a(n, std::vector<int>(m)), b(n, std::vector<int>(m));
for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) std::cin >> a[i][j];
for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) std::cin >> b[i][j];
bool flag = true;
for (int i = 0; i < n / 2 && flag; i++) {
for (int j = 0; j < m / 2; j++) {
int A = a[i][j] ^ a[i][m - 1 - j] ^ a[n - 1 - i][j] ^ a[n - 1 - i][m - 1 - j];
int B = b[i][j] ^ b[i][m - 1 - j] ^ b[n - 1 - i][j] ^ b[n - 1 - i][m - 1 - j];
if (A != B) {
flag = false;
break;
}
}
}
if (flag) std::cout << "Yes" << endl; else std::cout << "No" << endl;
}
signed main() {
IOS;
int t;
std::cin >> t;
while (t--) solve();
return 0;
}