D. Vasya And The Matrix

题目链接

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

题解

中档题。题意大概是求出一个矩阵,满足每一行与每一列的异或值为给定值。
首先判断给定的行异或值与列异或值的异或值是否相同,之后以如下方式构造矩形

  • 除了最后一行与最后一列其他值全赋值为 0
  • 对最后一行和最后一列根据给定数据作奇偶校验。

代码

#include<iostream>
using namespace std;
int ans[105][105];
int r[105], c[106];
int n, m, i, j, k, pdr = 0, pdc = 0;
int main()
{
    cin >> n >> m;
    for (i = 1; i <= n; i++) {
        cin >> r[i];
        ans[i][m] = r[i];
        pdr ^= r[i];
    }
    for (i = 1; i <= m; i++) {
        cin >> c[i];
        ans[n][i] = c[i];
        pdc ^= c[i];
    }
    if (pdr != pdc) {
        cout << "NO" << endl;
        return 0;
    }
    ans[n][m] = c[m] ^ (pdr^r[n]);
    cout << "YES" << endl;
    for (i = 1; i <= n; i++) {
        for (j = 1; j <= m; j++)
            cout << ans[i][j] << (j == m ? '\n' : ' ');
    }
}

标签: math, constructive algorithms, flows

添加新评论