0.96sec
笑而不语~
用了STL中的bitset. 在输入矩阵2的时候需要转置需要, 这样乘起来就方便了.
相乘之后通过判断1的个数 然后和矩阵3作比对 就可以判断对错, 提前跳出.
1是偶数的话, 相加必然是0, 所以对应矩阵3的位是1的话必然错误了. 1是奇数的话, 分析同理.
// Problem#: 1819
// Author#: Reid Chan
#include <cstdio>
#include <bitset>
using namespace std;
bitset<1000> a[1000], b[1000], c[1000];
void init(int n, char ch) {
char *str = new char [n];
for (int i = 0; i < n; i++) {
scanf("%s", str);
if (ch == 'a' || ch == 'c') {
for (int j = 0; j < n; j++) {
if (str[j] == '1') {
if (ch == 'a') { a[i].set(j); }
else { c[i].set(j); }
} else {
if (ch == 'a') { a[i].reset(j); }
else { c[i].reset(j); }
}
}
} else {
for (int j = 0; j < n; j++) {
if (str[j] == '1') { b[j].set(i); }
else { b[j].reset(i); }
}
}
}
}
bool is_match(int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if ((a[i] & b[j]).count() % 2 == 0) {
if (c[i][j] == 1) { return false; }
} else {
if (c[i][j] == 0) { return false; }
}
}
}
return true;
}
int main() {
int t; scanf("%d", &t);
while (t--) {
int n; scanf("%d", &n);
init(n, 'a');
init(n, 'b');
init(n, 'c');
if (is_match(n)) { printf("YES\n"); }
else { printf("NO\n"); }
}
return 0;
}