1. 程式人生 > >Vasya And The Matrix CodeForces - 1016D (思維+構造)

Vasya And The Matrix CodeForces - 1016D (思維+構造)

emp false void inf rip 等於 sim lar first

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 a

1, 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 a

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


題意:
有一個n*m的矩陣,每一個方格中有一個數。
現在給你 兩組數
第一組,有n 個數 每一個數i代表 第i行的所有數異或起來的數值。
第二組,有m 個數 每一個數j代表 第j列的所有數異或起來的數值。
問你是否存在一個矩陣,使之滿足上面的條件,如果存在,請輸出任意一個。

思路:
很好的一個構造題,
技術分享圖片

我們來看上圖的情況,ai代表是列的異或值,bi代表是那一列的異或值。
那麽
a1=x1^x4
a2=x2^x5
a3=x3^x6

b1=x1^x2^x3
b2=x4^x5^x6

a1^a2^a3=x1^x2^x3^x4^x5^x6
b1^b2=x1^x2^x3^x4^x5^x6

所以我們看數組a和數組b的分別異或起來的值是否相等,如果相等就存在答案,反而反之。

我們再來看如何構造出答案數組,
我們可以以一下的方式構造最為簡單,

技術分享圖片

把最後一行賦值為a[i] ,最後一列賦值為b[j] ,其他全賦值為0(因為0異或任意值x等於x)
,然後最右下角的數x賦值為 a1^a2^a3....^a(m-1) ^ bn
根據異或的性質就可以推算出 這個x就既滿足行又滿足列的關系。
例如行上, x ^ (a1~a m-1 ) = bn , 然後按照這個方法實現code就可以了,細節見代碼。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), ‘\0‘, sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
inline void getInt(int* p);
const int maxn=1010;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
ll a[maxn];
ll b[maxn];
ll ans[102][103];
int main()
{
    //freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
    //freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);
    ll n,m;
    cin>>n>>m;
    ll t1=0ll;
    ll t2=0ll;
    repd(i,1,n)
    {
        cin>>a[i];
        t1^=a[i];
    }
    repd(i,1,m)
    {
        cin>>b[i];
        t2^=b[i];
    }
    if(t1!=t2)
    {
        cout<<"NO"<<endl;
    }else
    {
        repd(i,1,n)
        {
            ans[i][m]^=a[i];
        }
        repd(j,1,m)
        {
            ans[n][j]^=b[j];
        }
        t2^=b[m];
        t2^=a[n];
        ans[n][m]=t2;
        cout<<"YES"<<endl;
        repd(i,1,n)
        {
            repd(j,1,m)
            {
                cout<<ans[i][j]<<" ";
            }
            cout<<endl;
        }
        
    }
    
    
    return 0;
}

inline void getInt(int* p) {
    char ch;
    do {
        ch = getchar();
    } while (ch ==   || ch == \n);
    if (ch == -) {
        *p = -(getchar() - 0);
        while ((ch = getchar()) >= 0 && ch <= 9) {
            *p = *p * 10 - ch + 0;
        }
    }
    else {
        *p = ch - 0;
        while ((ch = getchar()) >= 0 && ch <= 9) {
            *p = *p * 10 + ch - 0;
        }
    }
}



Vasya And The Matrix CodeForces - 1016D (思維+構造)