[CodeForces] 1016D 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 n n rows and m m m columns. For each row, he knows the xor (bitwise excluding or) of the elements in this row. The sequence a1,a2,...,an a_{1},a_{2},...,a_{n} a1,a2,...,an denotes the xor of elements in rows with indices 1 1 1 , 2 2 2 , ..., n n n , respectively. Similarly, for each column, he knows the xor of the elements in this column. The sequence b1,b2,...,bm b_{1},b_{2},...,b_{m} b1,b2,...,bm denotes the xor of elements in columns with indices 1 1 1 , 2 2 2 , ..., m m m , respectively.

Help Vasya! Find a matrix satisfying the given constraints or tell him that there is no suitable matrix.

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

输入输出格式

输入格式:

The first line contains two numbers n n n and $ m (2<=n,m<=100) $ — the dimensions of the matrix.

The second line contains n n n numbers $ a_{1},a_{2},...,a_{n} (0<=a_{i}<=10^{9}) $ , where ai a_{i} ai is the xor of all elements in row i i i .

The third line contains m m m numbers $ b_{1},b_{2},...,b_{m} (0<=b_{i}<=10^{9}) $ , where bi b_{i} bi is the xor of all elements in column i i 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 n n rows of m m m numbers in each $ c_{i1},c_{i2},... ,c_{im} (0<=c_{ij}<=2·10^{9}) $ — the description of the matrix.

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

输入输出样例

输入样例#1:
2 3
2 9
5 3 13
输出样例#1:
YES
3 4 5
6 7 8
输入样例#2:
3 3
1 7 6
2 15 12
输出样例#2:NO

题目解析

显然构造题啊,因为0 ^ n = n,所以手玩样例就可以发现我们只要最后一行填a[],最后一列填b[],其他全填0就好了。
这道题的难点在于处理角上的那个数(即a,b的交点),这个点暴力算出来就好了(b的异或和)。
至于无解的情况,我们用a的异或和 和 b的异或和向比较,如果不相等就无解了。
证明很简单,只要时刻记着0 ^ n = n就可以了。

Code

#include<iostream>
#include<cstdio>
using namespace std;

const int MAXN = 100 + 5;

int n,m;
int a[MAXN],b[MAXN];
int ans1,ans2;

int main() {
    scanf("%d%d",&n,&m);
    for(int i = 1;i <= n;i++) {
        scanf("%d",&a[i]);
        ans1 ^= a[i];
    }
    for(int i = 1;i <= m;i++) {
        scanf("%d",&b[i]);
        ans2 ^= b[i];
    }
    if(ans1 ^ ans2) {
        printf("NO
");
        return 0;
    } else printf("YES
");
    for(int i = 1;i < n;i++) {
        for(int j = 1;j < m;j++) printf("0 ");
        printf("%d
",a[i]);
    }
    for(int i = 1;i < m;i++) {
        printf("%d ",b[i]);
    }
    int ans = b[m];
    for(int i = 1;i < n;i++) ans ^= a[i];
    printf("%d",ans);
    return 0;
}
原文地址:https://www.cnblogs.com/floatiy/p/9482959.html