1819. Matrix
Constraints
Time Limit: 1 secs, Memory Limit: 32 MB
Description
Neo finds two matrices (these are really matrices, and they are binary). He needs a key to save the world, which is the product of the matrices (be careful, now everything is operating in binary, and the resulting matrix should be also in binary). Neo is good at computing and find out the solution within seconds. Your mission is to check if his answer is correct.
Input
First line of input will be a integer T (T<=15), indicating there are T test cases. Each test case consist of an integer n (1<=n<=1000) and three n*n matrices. The matrices are represented in n lines, each line consisting of n ‘0’ or ‘1’ (without any white spaces). The first two matrices are the matrices that Neo sees and the third one is the product calculated by Neo.
There are no blank lines between adjacent test cases.
Output
For each test case, output one word “YES” or “NO” in a line, depending on whether Neo’s answer is correct.
Sample Input
2 2 10 01 11 10 11 01 3 111 111 000 100 010 001 111 111 000
Sample Output
NO YES
Hint
For those one who forgets how to multiply matrices, here is the formula: Suppose A={aij}, B={bij}, A*B=C Then cij=sigma(aik*bkj), for k=1 to n In this problem, don’t forget to take the module 2 operation after summation.
用来学习bitset的一道题,怒贡献10余次WA,最后居然让我1s过- -:
#include <stdio.h>
#include <bitset>
using namespace std;
int main() {
char temp;
int case_num, i, j, n;
bool is_ok;
scanf("%d", &case_num);
while (case_num--) {
scanf("%d", &n);
bitset<1010> a[1010], b[1010], his_ans[1010];
for (i = 0; i < n; i++) {
a[i].reset();
b[i].reset();
his_ans[i].reset();
}
for (i = 0; i < n; i++) {
getchar();
for (j = 0; j < n; j++) {
temp = getchar();
if (temp == '1')
a[i].set(j);
}
}
for (i = 0; i < n; i++) {
getchar();
for (j = 0; j < n; j++) {
temp = getchar();
if (temp == '1')
b[j].set(i);//这里注意是在读取的时候就已经倒置了,方便后面的处理
}
}
for (i = 0; i < n; i++) {
getchar();
for (j = 0; j < n; j++) {
temp = getchar();
if (temp == '1')
his_ans[i].set(j);
}
}
is_ok = true;
for (i = 0; i < n && is_ok; i++) {
for (j = 0; j < n && is_ok; j++) {
if ((a[i] & b[j]).count() % 2) {
if (his_ans[i][j] == 0) {
is_ok = false;
break;
}
} else {
if (his_ans[i][j] == 1) {
is_ok = false;
break;
}
}
}
}
if (is_ok)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}