Codeforces Round #553 (Div. 2) B题

题目网址:http://codeforces.com/contest/1151/problem/B

题目大意:给定一个n*m的矩阵,问是否可以从每一行中选择一个数,使得这n个数异或大于0,如果可以还要输出它们的列位置

题解:首先如果a^b==0,b!=c,则a^c>0.那么考虑构造,为了方便,选取第一列的数,如果异或>0,直接输出列位置,反之则随便在一行中,找到一个与第一个不相等的数,那么由异或性质满足条件,如果n行都找不到,则无法实现。

 1 #include<bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 const int maxn=5e2+7;
 5 int a[maxn][maxn];
 6 int main()
 7 {
 8     int n,m;
 9     std::ios::sync_with_stdio(false);
10     cin>>n>>m;
11     for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) cin>>a[i][j];
12     int ans=a[1][1];
13     for(int i=2;i<=n;i++) ans^=a[i][1];
14     if(ans) {
15         cout<<"TAK"<<endl;
16         for(int i=1;i<=n;i++) printf("1 ");puts(" ");
17         return 0;
18     } 
19     int flag=0,x,y;
20     for(int i=1;i<=n;i++) {
21         for(int j=2;j<=m;j++) {
22             if(a[i][1]!=a[i][j]) {
23                 x=i,y=j;flag=1;
24             }
25         }
26     }    
27     if(flag) {
28         cout<<"TAK"<<endl;
29         for(int i=1;i<=x-1;i++) printf("1 ");
30         printf("%d ",y);
31         for(int i=x+1;i<=n;i++) printf("1 ");puts(" ");
32         return 0;
33     }
34 
35     cout<<"NIE"<<endl;
36 }  
View Code

猜你喜欢

转载自www.cnblogs.com/duxing201806/p/10778170.html