D. Vasya And The Matrix
Posted zllwxm123
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了D. Vasya And The Matrix相关的知识,希望对你有一定的参考价值。
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.
2 3
2 9
5 3 13
YES
3 4 5
6 7 8
3 3
1 7 6
2 15 12
NO
要使得矩阵存在,那么行和列的异或一定等于零.
或者说行的异或和等于列的异或和.
之后就直接特性的填充.
1 #include <bits/stdc++.h> 2 #define ll long long int 3 #define inf 0x3f3f3f3f 4 #define N 1005 5 using namespace std; 6 ll a[N],b[N]; 7 ll xn[N][N]; 8 ll n,m; 9 int main(){ 10 cin>>n>>m; 11 ll xx = 0; 12 for(int i = 0;i<n;i++){ 13 cin>>a[i]; 14 xx = xx^a[i]; 15 } 16 ll yy = 0; 17 for(int i = 0;i<m;i++){ 18 cin>>b[i]; 19 yy = yy^b[i]; 20 } 21 if(xx == yy){ 22 cout<<"YES"<<endl; 23 }else{ 24 cout<<"NO"<<endl; 25 return 0; 26 } 27 xx = xx^a[0]; 28 xx = xx^b[0]; 29 xn[0][0] = xx; 30 for(int i=1;i<m;i++) 31 xn[0][i] = b[i]; 32 for(int i = 1;i<n;i++){ 33 xn[i][0] = a[i]; 34 } 35 for(int i=0;i<n;i++){ 36 for(int j = 0;j<m;j++){ 37 cout<<xn[i][j]<<" "; 38 } 39 cout<<endl; 40 } 41 return 0; 42 }
以上是关于D. Vasya And The Matrix的主要内容,如果未能解决你的问题,请参考以下文章