D. Vasya And The Matrix

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

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 ≤ 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.

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·109) — the description of the matrix.

If there are several suitable matrices, it is allowed to print any of them.

Examples

input

Copy

2 32 95 3 13

output

Copy

YES3 4 56 7 8

input

Copy

3 31 7 62 15 12

output

Copy

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 }

原文地址:https://www.cnblogs.com/zllwxm123/p/9425069.html

时间: 2024-10-15 13:23:15

D. Vasya And The Matrix的相关文章

D. Vasya And The Matrix(Educational Codeforces Round 48)

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 teache

CodeForces - 1016D Vasya And The Matrix

题面在这里! 很明显二进制每一位都是无关的,所以可以先把原问题简化:给矩阵中的每个位置填入0/1,使得特定行/列有奇数个1,其他行/列有偶数个1. 一个比较好想的方法是对行和列 列出 n+m 个异或方程,其中有 n*m 个变量,随便求出一组解就好了(如果有的话). 但这个貌似并不是很好写... 可以把解异或方程转化成 在一个完全二分图(左n个点,右m个点)上选边,每个点可以是黑的(对应行/列要求有奇数个1)或者白的(反之),每选一条边就要把两端的点的黑白性颠倒. 然后发现这是一个经典问题,显然选

CF1042E Vasya and Magic Matrix

感觉不会期望. 首先把所有格子按照权值从小到大排一下序,这样一共有$n * m$个元素,每个元素有三个属性$x, y, val$. 下文中的下标均为排序后的下标. 这样子我们就可以推出公式: $f_i = \frac{1}{k}\sum_{j = 1}^{k}(f_j + (x_j - x_i)^2 + (y_j - y_i)^2)$    $($保证$val_j < val_i$并且这样的元素一共有$k$个$)$. 暴力转移是$n^2$的,但是我们可以把这个式子拆开: $f_i = \frac

Vasya and Magic Matrix CodeForces - 1042E (概率dp)

大意:给定n*m矩阵, 初始位置(r,c), 每一步随机移动到权值小于当前点的位置, 得分为移动距离的平方, 求得分期望. 直接暴力dp的话复杂度是O(n^4), 把距离平方拆开化简一下, 可以O(n^2logn). #include <iostream> #include <sstream> #include <algorithm> #include <cstdio> #include <math.h> #include <set>

Restoring Numbers

D. Restoring Numbers Vasya had two arrays consisting of non-negative integers: a of size n and b of size m. Vasya chose a positive integerk and created an n × m matrix v using the following formula: Vasya wrote down matrix v on a piece of paper and p

Educational Codeforces Round 48

Educational Codeforces Round 48 C.Vasya And The Mushrooms 思路很简单,走法有一个统一形式就是先上下走,然后到某个位置左右一个来回.然后就推一下,后边那段的递推式子,枚举改变走法的位置即可.看出做法之后发现要推个式子,于是跑去写D了...然后D一开始思路错了...凉啊 #include <bits/stdc++.h> #define rep(i,a,b) for(int i=a;i<=b;++i) #define per(i,a,b

Educational Codeforces Round 48 (Rated for Div. 2) - 赛后补题

C. Vasya And The Mushrooms 题解:拿笔画他的行走路线,你会发现可以前缀和预处理一些东西出来. const int N = 300005; int n; ll a[N], b[N], dp[2][N], sp[2][N], sum[N]; int get_a() { dp[0][0] = 0; for (int i = 1; i < n; ++i) { dp[0][i] = dp[0][i - 1] + 1ll * i * a[i]; } int t = n; dp[1]

Codeforces Edu Round 48 A-D

A. Death Note 简单模拟,可用\(\%\)和 \(/\)来减少代码量 #include <iostream> #include <cstdio> using namespace std; const int N = 200010; int n, m, a[N], cnt = 0, tot = 0; int main(){ scanf("%d%d", &n, &m); for(int i = 1; i <= n; i++){ sc

hdu 5015 233 Matrix (矩阵快速幂)

题意: 有一种矩阵,它的第一行是这样一些数:a  0,0 = 0, a 0,1 = 233,a 0,2 = 2333,a 0,3 = 23333... 除此之外,在这个矩阵里, 我们有 a i,j = a i-1,j +a i,j-1( i,j ≠ 0).现在给你 a 1,0,a 2,0,...,a n,0, 你能告诉我a n,m 是多少吗? n,m(n ≤ 10,m ≤ 10 9)输出 a n,m mod 10000007. 思路:首先我们观察n和m的取值范围,会发现n非常小而m却非常大,如果