跳到正文

题解

Niton & Matrix 题解

xi_yj_d约 4 分钟
原文
文章信息与版本

文章信息

发布
2025年6月18日
收录
2026年2月22日
更新
2026年11月11日
篇幅
约 4 分钟

版本

  1. v1当前

注意到原操作等价于将关于矩阵中轴线对称的两个格子同时取反,在中轴线上的格子任意取值。

证明:

取反第 jj 列,翻转第 ii 行,取反第 jj 列,翻转第 ii 行即可将 ai,j,ai,m+1−ja_{i,j},a_{i,m+1-j} 同时取反,ai,j,an+1−i,ja_{i,j},a_{n+1-i,j} 同理。

于是中轴线上的格子也是可以取反的,将整行取反后再将其它位置取反即可。

使用该操作也可以实现原操作。取反显然可以实现,翻转时若 ai,j=ai,m+1−ja_{i,j}=a_{i,m+1-j} 则不会发生变化,否则相当于同时取反,列同理。

原操作与该操作可以相互推导,因此等价。

于是矩阵中的格子构成了若干个自环和四元环,每条边上的两个格子可以同时取反。

自环显然不需考虑。容易发现四元环中的格子可以在不改变 1 个数的奇偶性的情况下任意调整,因此判断 1 个数奇偶性是否相同即可。

C++
#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;
}
原文
存档评论2 条抓取于