Qualification Rounds
Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of n problems, and they want to select any non-empty subset of it as a problemset.
k experienced teams are participating in the contest. Some of these teams already know some of the problems. To make the contest interesting for them, each of the teams should know at most half of the selected problems.
Determine if Snark and Philip can make an interesting problemset!
Input
The first line contains two integers n, k (1 ≤ n ≤ 105, 1 ≤ k ≤ 4) — the number of problems and the number of experienced teams.
Each of the next n lines contains k integers, each equal to 0 or 1. The j-th number in the i-th line is 1 if j-th team knows i-th problem and 0 otherwise.
Output
Print “YES” (quotes for clarity), if it is possible to make an interesting problemset, and “NO” otherwise.
You can print each character either upper- or lowercase (“YeS” and “yes” are valid when the answer is “YES”).
这道题思维还是比较难想的;
首先可以发现,k<=4,这说明最多有24种01排列组合;再观察会发现,只要找到2组01排列组合,它们在相同位的值是不一样的,就一定存在;如果没有找到,那么就肯定不存在;
可以把01排列转化为10进制数,然后直接&判断是否为0就行;
#include<bits/stdc++.h>
#define ll long long
#define pa pair<int,int>
#define lson k<<1
#define rson k<<1|1
#define inf 0x3f3f3f3f
//ios::sync_with_stdio(false);
using namespace std;
const int N=200100;
const int M=1000100;
const ll mod=1e9+7;
bool vis[20];
int main(){
ios::sync_with_stdio(false);
int n,k;
cin>>n>>k;
for(int i=1;i<=n;i++){
int a=0;
for(int j=1;j<=k;j++){
int x;
cin>>x;
a*=2;
a+=x;
}
vis[a]=true;
}
int ok=0;
for(int i=0;i<=(1<<k);i++){
if(!vis[i]) continue;
for(int j=0;j<=(1<<k);j++){
if(!vis[j]||(i&j)) continue;
ok=1;
break;
}
}
if(ok) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
return 0;
}