当前位置 : 主页 > 编程语言 > c语言 >

Educational Codeforces Round 48 (Rated for Div. 2) D. Vasya And The Matrix

来源:互联网 收集:自由互联 发布时间:2023-09-06
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

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
inputCopy
2 3
2 9
5 3 13
outputCopy
YES
3 4 5
6 7 8
inputCopy
3 3
1 7 6
2 15 12
outputCopy
NO
题意:给你一个矩阵每行,每列的异或和,如果可以构造出原矩阵,输出一种情况;否则输出NO

思路:判断 yes or no 很容易,至于构造,感觉只要做过类似的就应该会做了——-构造 n-1 * m-1 的矩阵,这里面值随便填,然后在最后一行,一列按题目要求求出即可。(套路题。。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<string>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<sstream>
typedef long long ll;
using namespace std;
typedef unsigned long long int ull;
#define maxn 20005
#define ms(x) memset(x,0,sizeof(x))
#define Inf 0x7fffffff
#define inf 0x3f3f3f3f
const long long int mod = 1e9 + 7;
#define pi acos(-1.0)
#define pii pair<int,int>
#define eps 1e-6
#define pll pair<ll,ll>



ll quickpow(ll a, ll b) {
    ll ans = 1;
    while (b > 0) {
        if (b % 2)ans = ans * a;
        b = b / 2;
        a = a * a;
    }
    return ans;
}

int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a%b);
}

int rr[maxn], cc[maxn];
int a[500][500];


int main() {
    ios::sync_with_stdio(false);
    int n, m;
    cin >> n >> m;
    int i, j;
    int sum1 = 0, sum2 = 0;
    for (i = 0; i < n; i++) {
        cin >> rr[i];
        if (i == 0)sum1 = rr[i];
        else sum1 ^= rr[i];
    }
    for (i = 0; i < m; i++) {
        cin >> cc[i];
        if (i == 0)sum2 = cc[i];
        else sum2 ^= cc[i];
    }
    if (sum1 != sum2) {
        cout << "NO" << endl;
    }
    else {
        cout << "YES" << endl;
        for (i = 0; i < n - 1; i++) {
            for (j = 0; j < m - 1; j++) {
                a[i][j] = 0;
            }
        }
        for (i = 0; i < n; i++) {
            int k = a[i][0];
            for (j = 1; j < m - 1; j++) {
                k ^= a[i][j];
            }
            a[i][m - 1] = k ^ rr[i];
        }
        for (i = 0; i < m ; i++) {
            int k = a[0][i];
            for (j = 1; j < n - 1; j++)
                k ^= a[j][i];
            a[n - 1][i] = k ^ cc[i];
        }
        for (i = 0; i < n; i++) {
            for (j = 0; j < m; j++) {
                cout << a[i][j] << ' ';
            }
            cout << endl;
        }
    }
}
网友评论