http://codeforces.com/contest/1013/problem/B
题意:给出一个数x和n个数,问这n个数能否通过与x进行&操作出现两个相同的数
若能输出最短的变化次数
有这几种情况:
0次:n个数中原本就有相同的数
1次:一个数通过一次变化变成了另一个数
2次:有两个原本不同的数通过各一次变换变成了同一个数
无解:不论如何变换不会出现相同的数
#include <bits/stdc++.h>
using namespace std;
int main()
{
multiset<int> s1, s2;
set<int> s3, s4;
int n, k;
int flag = 0;
while (cin >> n >> k) {
s1.clear();
s2.clear();
s3.clear();
s4.clear();
flag=0;
int num;
for (int i = 0; i < n; i ++) {
scanf("%d",&num);
s1.insert(num);
s3.insert(num);
if ((num&k) != num) {
s2.insert(num&k);
s4.insert(num&k);
}
}
if (s1.size() != s3.size()) {
cout << '0' << endl;
continue;
}
for (auto it = s1.begin(); it != s1.end(); it ++) {
if (s4.count(*it)) {
flag = 1;
cout << '1' << endl;
break;
}
}
if (flag) continue;
if (s2.size() != s4.size()) {
cout << '2' << endl;
continue;
}
cout << "-1" << endl;
}
return 0;
}