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

直接构造一个任意的(n-1)*(m-1)的0矩阵,最后一个值异或得到

#include <iostream>
#include <algorithm>
#include <string.h>
#include <stdio.h>
using namespace std;
#define ll long long
int main(int argc, char const *argv[])
{
    int n, m;
    int r, c;
    int a[105], b[105];

    cin >> n >> m;

    r = c = 0;

    for (int i = 0; i < n; i++) {
        cin >> a[i];
        r ^= a[i];
    }
    for (int i = 0; i < m; i++) {
        cin >> b[i];
        c ^= b[i];
    }
    if (r != c) {
        puts("NO");
    } else {
        bool first;
        puts("YES");
        for (int i = 0; i < n; i++) {
            first = true;
            for (int j = 0; j < m; j++) {
                if (!first) printf(" ");
                else first = false;
                if (i != n-1 && j != m-1) {
                    printf("0");
                } else if (j == m-1 && i == n-1) {
                    r = 0;
                    for (int k = 0; k < m-1; k++) r ^= b[k];
                    r ^= a[n-1];
                    printf("%d", r);
                } else if (j == m-1){
                    printf("%d", a[i]);
                } else {
                    printf("%d", b[j]);
                }
            }
            printf("\n");
        }
    }

    return 0;
}

原文地址:https://www.cnblogs.com/huangjiaming/p/9420472.html

时间: 2024-10-10 11:45:54

D. Vasya And The Matrix(Educational Codeforces Round 48)的相关文章

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 1082 A. Vasya and Book-题意 (Educational Codeforces Round 55 (Rated for Div. 2))

A. Vasya and Book time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Vasya is reading a e-book. The file of the book consists of nn pages, numbered from 11 to nn. The screen is currently disp

Educational Codeforces Round 48 (Rated for Div. 2)

http://codeforces.com/contest/1016 A. 没想到这个也会TLE,太粗心了 B. 暴力就好了,多情况讨论又出错... 思路跟我一样的解法   为什么我做了那么多讨论,原因是没注意这个: 标记 最后一个字符,同时注意 l+m-3. 特殊情况就 vis 0000011111111111112 s1  abaccabaacabacabacca      红色的地方是 l 和 r ,为了防止在 l 处计数多了就得 l + m - 3 s2  abacca we[s+m-1

Educational Codeforces Round 48 (Rated for Div. 2) B Segment Occurrences

翻译 给你一个字符串\(s\)和另一个字符串\(t\),然后给你\(q\)个区间,问\(s\)在这些区间里的子串有多少个与\(t\)相同. 思路 一道要细心的模拟题,使用\(STL string\),暴力,前缀和,\(Hash\),\(Kmp\)都能做出来,然后我来介绍一下用 \(vector\)的做法. 首先预处理\(s\),从头到位找到每一个长度是字符串t的长度\(m\)的字符串,如果其与\(t\)相等,那么就往vector中压入\(1\),否则压入\(0\),这样,我们就找到每个编号的字符

Educational Codeforces Round 48 (Rated for Div. 2)G. Appropriate Team

题意:求满足条件的(i,j)对数:\(gcd(v,a_i)=x,lcm(v,a_j)=y\) 题解:\(x|a_i,a_j|y\),\(x|y\),考虑质因子p,假设a_i中p次数为a,x中次数为b,y为c,\(a_j\)为d;a>=b,c>=d. 假设a>b,c>d,那么由于\(gcd(v,a_i)=x\),v中p的次数为b,由于\(lcm(v,a_j)=y\),那么\(max(b,d)==c\),又c>d,所以b=c<a和x|y矛盾,所以此时ij不满足条件 其他情况

CodeForces - 837E - Vasya&#39;s Function | Educational Codeforces Round 26

/* CodeForces - 837E - Vasya's Function [ 数论 ] | Educational Codeforces Round 26 题意: f(a, 0) = 0; f(a, b) = 1 + f(a, b-gcd(a, b)); 求 f(a, b) , a,b <= 1e12 分析: b 每次减 gcd(a, b) 等价于 b/gcd(a,b) 每次减 1 减到什么时候呢,就是 b/gcd(a,b)-k 后 不与 a 互质 可先将 a 质因数分解,b能除就除,不能

Educational Codeforces Round 26 E - Vasya&#39;s Function

数论题还是好恶心啊. 题目大意:给你两个不超过1e12的数 x,y,定义一个f ( x, y ) 如果y==0 返回 0 否则返回1+ f ( x , y - gcd( x , y ) ); 思路:我们设gcd ( x , y) 为G,那么 设 x  = A*G,y = B*G,我们考虑减去多少个G时x y 的gcd会改变,我们设减去 k个G的时候 x和y 的gcd为改变,即 A*G 和 ( B - k ) * G 的 gcd 改变了,什么情况下会改变呢,就是A 和( B -  k )的gcd

Educational Codeforces Round 40 C. Matrix Walk( 思维)

Educational Codeforces Round 40 (Rated for Div. 2) C. Matrix Walk time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output There is a matrix A of size x?×?y filled with integers. For every , *A**i,?