题目链接:
题意:
有整数n,x;
有n个整数a1,a2...an,可以对ai进行任意次ai&x;
求经过多少次操作可以使,整数中有两个是相同的。
思路:
占坑,等我想明白了再填。
AC代码如下:
#include <cstdio>
#include <iostream>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
using namespace std;
int main()
{
int n,x;
while(cin>>n>>x)
{
set<int>s;
set<int>s1;
int a[100001];
for(int i=0;i<n;i++)
{
cin>>a[i];
s.insert(a[i]);
}
if(s.size()<n)//本来就有大于等于2个的值
{
cout<<0<<endl;
}
else
{
int flag=0;
for(int i=0;i<n;i++)
{
int k=a[i]&x;
if(k!=a[i]&&s.count(k))//&后改变并且原来就存在了,所以只需要1步操作
{
flag=1;
break;
}
}
if(flag==1)
{
cout<<1<<endl;
}
else
{
for(int i=0;i<n;i++)
{
int k=a[i]&x;//&后有两个一样的,所以只需要2步操作
if(s1.count(k))
{
flag=2;
break;
}
s1.insert(k);
}
if(flag==2)
{
cout<<2<<endl;
}
else
{
cout<<-1<<endl;
}
}
}
}
return 0;
}