D. Vasya And The Matrix
2 seconds
256 megabytes
standard input
standard output
Now Vasya is taking an exam in mathematics. In order to get a good mark, Vasya needs to guess the matrix that the teacher has constructed!
Vasya knows that the matrix consists of n rows and m columns. For each row, he knows the xor (bitwise excluding or) of the elements in this row. The sequence a1, a2, ..., an denotes the xor of elements in rows with indices 1, 2, ..., n, respectively. Similarly, for each column, he knows the xor of the elements in this column. The sequence b1, b2, ..., bm denotes the xor of elements in columns with indices 1, 2, ..., m, respectively.
Help Vasya! Find a matrix satisfying the given constraints or tell him that there is no suitable matrix.
The first line contains two numbers n and m (2 ≤ n, m ≤ 100) — the dimensions of the matrix.
The second line contains n numbers a1, a2, ..., an (0 ≤ ai ≤ 109), where ai is the xor of all elements in row i.
The third line contains m numbers b1, b2, ..., bm (0 ≤ bi ≤ 109), where bi is the xor of all elements in column i.
If there is no matrix satisfying the given constraints in the first line, output "NO".
Otherwise, on the first line output "YES", and then n rows of m numbers in each ci1, ci2, ... , cim (0 ≤ cij ≤ 2·109) — the description of the matrix.
If there are several suitable matrices, it is allowed to print any of them.
Examples
2 3
2 9
5 3 13
YES
3 4 5
6 7 8
3 3
1 7 6
2 15 12
Output
NO
题目大意:a[i] 表示第i行的异或和,b[j] 表示第j列的异或和,然后给出a[i] 和 b[j],存在这样的矩阵就构造出任意一个,不存在就NO
对于我这种位运算知识为0的来说,再简单也不会。后来看了聚聚们说这道题很水,我看了看题解。首先存在条件:因为 a[i]是所有行的异或和 b[i]是所有列的异或和 ,都是所有元素异或和,应该相等,x ^ x = 0; 然后就控制每一行每一列,这里有一个性质:假如有f[i][j], 那么可以把它变为0, f[0][j] ^ f[i][0] ^ f[i][j] , 行和列的异或不变。
即 f[i][0] ^ f[0][j] = f[i][0] ^ f[0][j] ^ f[i][j]
所以转化为一行和一列即可。第一行第一列的数 是重点。a[0] = b[1] ^ b[2] ^ b[m]
所以 b[1] = b[2] ^ b[3] ^ ... b[m] ^ a[0] (1 ^ 2 = 3) -- > (2 ^ 3 = 1)
1 #include2 #include 3 #include 4 #include <string.h> 5 #include 6 #include <string> 7 #include 8 #include <set> 9 #include