zoukankan      html  css  js  c++  java
  • Educational Codeforces Round 48 (Rated for Div. 2) D 1016D Vasya And The Matrix (构造)

    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 3
    2 9
    5 3 13
    Output
    Copy
    YES
    3 4 5
    6 7 8
    Input
    Copy
    3 3
    1 7 6
    2 15 12

    Output

    Copy
    NO

    题目大意:a[i] 表示第i行的异或和,b[j] 表示第j列的异或和,然后给出a[i] 和 b[j],存在这样的矩阵就构造出任意一个,不存在就NO

    对于我这种位运算知识为0的来说,再简单也不会。后来看了聚聚们说这道题很水,我看了看题解。首先存在条件:因为 a[i]是所有行的异或和 b[i]是所有列的异或和  ,都是所有元素异或和,应该相等,x ^ x = 0; 然后就控制每一行每一列,这里有一个性质:假如有f[i][j], 那么可以把它变为0, f[0][j] ^ f[i][0] ^ f[i][j] , 行和列的异或不变。

    即  f[i][0] ^ f[0][j] = f[i][0] ^ f[0][j] ^ f[i][j] 

    所以转化为一行和一列即可。第一行第一列的数 是重点。a[0] = b[1] ^ b[2] ^ b[m]

    所以 b[1] = b[2] ^ b[3] ^ ... b[m]  ^ a[0]    (1 ^ 2 = 3) -- >  (2 ^ 3 = 1)

     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <math.h>
     4 #include <string.h>
     5 #include <stdlib.h>
     6 #include <string>
     7 #include <vector>
     8 #include <set>
     9 #include <map>
    10 #include <queue>
    11 #include <algorithm>
    12 #include <sstream>
    13 #include <stack>
    14 using namespace std;
    15 typedef long long ll;
    16 const int inf = 0x3f3f3f3f;
    17 const int N = 100 + 7;
    18 int a[N], b[N];
    19 
    20 int main() {
    21     //freopen("in.txt", "r", stdin);
    22     int n, m;
    23     scanf("%d%d", &n, &m);
    24     int cur = 0;
    25     for(int i = 0; i < n; i++) { 
    26         scanf("%d", &a[i]);
    27         cur ^=  a[i];
    28     } 
    29     for(int i = 0; i < m; i++) {
    30         scanf("%d", &b[i]);
    31         cur ^= b[i];
    32     }
    33     if(cur != 0) {//因为 a[i]是所有行的异或和 b[i]是所有列的异或和 
    34         printf("NO
    ");// 都是所有元素异或和,应该相等,x ^ x = 0; 
    35         return 0;
    36     } 
    37     printf("YES
    ");
    38     for(int i = 1; i < m; i++)//求第一行第一列 ****重点***** 
    39         cur ^= b[i];
    40     cur ^= a[0];
    41     printf("%d ", cur);
    42     for(int i = 1; i < m; i++) {//第一行 
    43         printf("%d ", b[i]);
    44     } 
    45     printf("
    ");
    46     for(int i = 1; i < n; i++) {//每一行第一列和剩余的 
    47         printf("%d ", a[i]);
    48         for(int j = 1; j < m; j++)
    49             printf("0 ");
    50         printf("
    ");
    51     }
    52 }
    
    
  • 相关阅读:
    Java实现 LeetCode 735 行星碰撞(栈)
    Java实现 LeetCode 735 行星碰撞(栈)
    Java实现 LeetCode 887 鸡蛋掉落(动态规划,谷歌面试题,蓝桥杯真题)
    Java实现 LeetCode 887 鸡蛋掉落(动态规划,谷歌面试题,蓝桥杯真题)
    Java实现 LeetCode 887 鸡蛋掉落(动态规划,谷歌面试题,蓝桥杯真题)
    Java实现 蓝桥杯算法提高 求最大值
    Java实现 蓝桥杯算法提高 求最大值
    Java实现 蓝桥杯算法提高 求最大值
    Python eval() 函数
    Python repr() 函数
  • 原文地址:https://www.cnblogs.com/ACMerszl/p/9572936.html
Copyright © 2011-2022 走看看