原题:
D. Vasya And The Matrix
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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.
Input
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 ≤ 10^9), where ai is the xor of all elements in row i.
The third line contains m numbers b1, b2, …, bm (0 ≤ bi ≤ 10^9), where bi is the xor of all elements in column i.
Output
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·10^9) — the description of the matrix.
If there are several suitable matrices, it is allowed to print any of them.
Examples
input
2 3
2 9
5 3 13
output
YES
3 4 5
6 7 8
input
3 3
1 7 6
2 15 12
output
NO
中文:
有一个n×m的矩阵,告诉你每一行的连续异或运算和每一列的连续异或运算结果,让你判断是否存在这样的矩阵,存在则输出YES并给出任意一个这样的矩阵,否则输出NO。
代码:
#include
using namespace std;
typedef long long ll;
int n,m;
int row[101],col[101];
int mat[101][101];
int main()
{
ios::sync_with_stdio(false);
while(cin>>n>>m)
{
memset(mat,0,sizeof(mat));
int tmp=0;
for(int i=1;i<=n;i++)
cin>>row[i];
for(int i=1;i<=m;i++)
cin>>col[i];
for(int i=2;i<=n;i++)
tmp^=row[i];
for(int i=1;i<=m;i++)
tmp^=col[i];
if(tmp!=row[1])
{
cout<<"NO"<continue;
}
else
{
cout<<"YES"<int ind=0;
for(int i=1;i0;
for(int j=1;jfor(int i=1;i<=m;i++)//列
{
tmp=0;
for(int j=1;j//行
tmp^=mat[j][i];
mat[n][i]=tmp^col[i];
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(j!=m)
cout<" ";
else
cout<return 0;
}
解答:
先复习一下异或运算的规则
如果 a⊕b⊕c=x a ⊕ b ⊕ c = x
那么 b⊕c=x⊕a b ⊕ c = x ⊕ a
a⊕b=b⊕a a ⊕ b = b ⊕ a
a⊕a=0 a ⊕ a = 0
0⊕a=a 0 ⊕ a = a
有了上面的几条,就可以就可以在纸上试试解方程啦!
最后解着解着,你会发现所有变量都消掉了,如果有3行3列,每一行的异或值是a,b,c,每一列的异或值是d,e,f
那么,如果满足 a⊕b⊕c⊕d⊕e=f a ⊕ b ⊕ c ⊕ d ⊕ e = f 那么这个矩阵就是存在的,否则不存在
矩阵里面随便填点数,最后一行和最后一列的数字满足行和列的要求即可。