Codeforces1016D题解

一道好玩的构造题

Description

已知一个n×m的矩阵,每行每列元素的异或和,请构造一个满足要求的矩阵。若不存在,输出”NO”,否则输出”YES”和矩阵。

$$n, m \leq 100$$

Samples

Input #1

1
2
3
2 3
2 9
5 3 13

Output #1

1
2
3
YES
3 4 5
6 7 8

Input #2

1
2
3
3 3
1 7 6
2 15 12

Output #2

1
NO

Solution

一道需要脑洞开大的好玩构造题~

考虑异或的性质$a \text{ } \rm{xor} \text{ } 0 = a$, 即一个数异或0之后的结果仍然是其本身.

那么我们把目标矩阵除了最后一列和最后一行, 都直接填上0.

这样的后果是, 除了右下角的元素, 其它的元素直接填上对应行/列的异或和即可.

右下角的元素可以通过异或的另一个性质$a \text{ } \rm{xor} \text{ } b = c \implies b = c \text{ } \rm{xor} \text{ } a$推出来.

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* Headers */
#include<algorithm>
#include<iostream>
#include<cstring>
#include<climits>
#include<cstdio>
#include<cctype>
#include<vector>
#include<cmath>
#include<array>
#include<queue>
#include<stack>
#include<map>
#define FOR(i,a,b,c) for(int i=(a);i<=(b);i+=(c))
#define ROF(i,a,b,c) for(int i=(a);i>=(b);i-=(c))
#define FORL(i,a,b,c) for(long long i=(a);i<=(b);i+=(c))
#define ROFL(i,a,b,c) for(long long i=(a);i>=(b);i-=(c))
#define FORR(i,a,b,c) for(register int i=(a);i<=(b);i+=(c))
#define ROFR(i,a,b,c) for(register int i=(a);i>=(b);i-=(c))
#define RevEdge(x) x^1
#define lowbit(x) x&(-x)
#define LeftChild(x) x<<1
#define RightChild(x) (x<<1)+1
#define CLOSE_IN() fclose(stdin);
#define CLOSE_OUT() fclose(stdout);
#define FILE_IN(x) freopen(x,"r",stdin);
#define FILE_OUT(x) freopen(x,"w",stdout);
#define IOS(x) std::ios::sync_with_stdio(x)
#define Dividing() printf("-----------------------------------\n");
namespace FastIO{
const int BUFSIZE = 1 << 20;
char ibuf[BUFSIZE],*is = ibuf,*its = ibuf;
char obuf[BUFSIZE],*os = obuf,*ot = obuf + BUFSIZE;
inline char getch(){
if(is == its)
its = (is = ibuf)+fread(ibuf,1,BUFSIZE,stdin);
return (is == its)?EOF:*is++;
}
inline int getint(){
int res = 0,neg = 0,ch = getch();
while(!(isdigit(ch) || ch == '-') && ch != EOF)
ch = getch();
if(ch == '-'){
neg = 1;ch = getch();
}
while(isdigit(ch)){
res = (res << 3) + (res << 1)+ (ch - '0');
ch = getch();
}
return neg?-res:res;
}
inline void flush(){
fwrite(obuf,1,os - obuf,stdout);
os = obuf;
}
inline void putch(char ch){
*os++ = ch;
if(os == ot) flush();
}
inline void putint(int res){
static char q[10];
if(res == 0) putch('0');
else if(res < 0){putch('-');res = -res;}
int top = 0;
while(res){
q[top++] = res % 10 + '0';
res /= 10;
}
while(top--) putch(q[top]);
}
inline void space(bool x){
if(!x) putch('\n');
else putch(' ');
}
}
inline void read(int &x){
int rt = FastIO::getint();
x = rt;
}
inline void print(int x,bool enter){
FastIO::putint(x);
FastIO::flush();
FastIO::space(enter);
}
/* definitions */
constexpr int MAXN = 1e2 + 10;
int n, m, ans;
std::array<int, MAXN> Iline{0}, Jline{0};
/* functions */
int main(int argc,char *argv[]){
scanf("%d %d", &n, &m);
FOR(i, 1, n, 1) scanf("%d", &Iline[i]), ans ^= Iline[i];
FOR(i, 1, m, 1) scanf("%d", &Jline[i]), ans ^= Jline[i];
if(ans != 0) return puts("NO") & 0;
printf("YES\n"); ans = Jline[m];
FOR(i, 1, n - 1, 1) {
FOR(j, 1, m - 1, 1) printf("0 ");
printf("%d\n", Iline[i]);
ans = ans ^ Iline[i];
}
FOR(i, 1, m - 1, 1) printf("%d ", Jline[i]);
printf("%d\n", ans);
return 0;
}

THE END