codeforces D. Vasya And The Matrix(思维+矩阵+异或)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wwwlps/article/details/81663443

题意:给定一个n*m的矩阵(未知),以及每一行的各个元素的异或和,每一列的各个元素的异或和,求出一个符合的矩阵(任意即可)

题意:思维很重要,考虑特例的话,只需要考虑最后一行和最后一列,除了最后一行和最后一列,矩阵的其他元素为0,最后,矩阵第n行和第m列之间存在一个方程关系,来求出最后一个元素

转载:

思路:对于这样一个矩阵,我们只需要考虑最后一行和最后一列即可,其他都用0填充。然后,最后一行和最后一列也只需要考虑第n行第m个即可。对于样例   行:2 ,9 列:5,3,13  .我们只需要构造出

                                                                                                                   0,0, 2

                                                                                                                   5,3,15 

这样一个矩阵即可,只需要考虑最后一个位置的数字即可,令最后一个数字为x的话,有

2^x=13,  5^3^x=9。很容易就可得出,x=13^2=9^5^3=15.

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include <vector>
#include<queue>
#include <stack>
#include <map>
#define maxn 605005
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
int n,m;
int a[105],b[105];
int main()
{
   cin>>n>>m;
   LL an,bm;
   an=bm=0;
   LL ax,by;
   for(int i=0;i<n;i++)
   {
      int t;
      cin>>t;
      a[i]=t;
      if(i!=n-1)//先求出第m列的前n-1个元素的异或
         an=an^t;
      else
         ax=t;
   }
   for(int i=0;i<m;i++)
   {
      int t;
      cin>>t;
      b[i]=t;
      if(i!=m-1)//先求出第n行的前m-1个元素的异或
         bm=bm^t;
      else
         by=t;
   }
   LL t1,t2;
   t1=an^by;
   t2=bm^ax;
   if(t1!=t2)//判断是否成立
   {
      cout << "NO\n";
   }
   else
   {
      cout << "YES\n";
      for(int i=0;i<n;i++)
      {
         for(int j=0;j<m;j++)
         {
            if(i<n-1&&j!=m-1)
               cout << 0 << ' ';
            else if(i!=n-1&&j==m-1)
            {
               cout << a[i];
            }
            else if(j!=m-1)
            {
               cout <<  b[j] << ' ';
            }
         }
         if(i!=n-1)
            cout << endl;
      }
      cout << t1 << endl;
   }
   return 0;
}

猜你喜欢

转载自blog.csdn.net/wwwlps/article/details/81663443