CodeForces - 1013B . And 想了一定时间

CodeForces - 1013B B. And

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

There is an array with n elements a1, a2, ..., an and the number x.

In one operation you can select some i (1 ≤ i ≤ n) and replace element ai with ai & x, where & denotes the bitwise and operation.

You want the array to have at least two equal elements after applying some operations (possibly, none). In other words, there should be at least two distinct indices i ≠ j such that ai = aj. Determine whether it is possible to achieve and, if possible, the minimal number of operations to apply.

Input

The first line contains integers n and x (2 ≤ n ≤ 100 000, 1 ≤ x ≤ 100 000), number of elements in the array and the number to and with.

The second line contains n integers ai (1 ≤ ai ≤ 100 000), the elements of the array.

Output

Print a single integer denoting the minimal number of operations to do, or -1, if it is impossible.

Examples

input

Copy

4 3
1 2 3 7

output

Copy

1

input

Copy

2 228
1 1

output

Copy

0

input

Copy

3 7
1 2 3

output

Copy

-1

Note

In the first example one can apply the operation to the last element of the array. That replaces 7 with 3, so we achieve the goal in one move.

In the second example the array already has two equal elements.

In the third example applying the operation won't change the array at all, so it is impossible to make some pair of elements equal.

题意:

给你n个数,给你一个m,用m&a【1……n】改变a【1……n】,保证至少有两个相同即可

分析:

先给一个规律,t=m&x,t=m&t,也就是说m&x,一次,两次,三次……都是一样的,所以说,改变的次数有0,1,2,否则则是-1.

0次好办,开个数组vis【a[i]】记录a【i】出现的次数。

1次,就一个for循环,枚举改变当前数值(m&a【i】),与vis【a[i]】如果大于二,就表明有。

2次,在开一个数组vis1记录m&a[i]的数量,跟0判断一样,如果vis【i】有一个大于2,则输出2,当然这么情况需要在0,和1都不成立的情况下。

上述情况都不成立,输出-1

#include<cstdio>  
#include<cstring>  
#include<cstdlib>  
#include<cctype>  
#include<cmath>  
#include<iostream>  
#include<sstream>  
#include<iterator>  
#include<algorithm>  
#include<string>  
#include<vector>  
#include<set>  
#include<map>  
#include<stack>  
#include<deque>  
#include<queue>  
#include<list>  
using namespace std;  
const double eps = 1e-8;  
typedef long long LL;  
typedef unsigned long long ULL;  
const int INT_INF = 0x3f3f3f3f;  
const int INT_M_INF = 0x7f7f7f7f;  
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;  
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;  
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};  
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};  
const int MOD = 1e9 + 7;  
const double pi = acos(-1.0);  
const int MAXN = 1e5 + 10;  
const int MAXT = 10000 + 10;  
#define N 10005
int n,m;
int a[100005];
int b[100005];
int vis[100005];
int vis1[100005];
int main()  
{  
    while(scanf("%d%d",&n,&m)!=EOF)  
    {  
    	int flag=0,flag1=0;
    	memset(vis,0,sizeof(vis));
    	memset(vis1,0,sizeof(vis1));
    	for(int i=1;i<=n;i++)
		{
			scanf("%d",&a[i]);
			b[i]=a[i]&m;
			vis[a[i]]++;
			vis1[b[i]]++;
			if(vis[a[i]]>=2)
			{
				flag=1;
			}
			if(vis1[b[i]]>=2)
			{
				flag1=1;
			}
		}
		if(flag==1)
		{
			cout<<0<<endl;
		}
		else
		{
			for(int i=1;i<=n;i++)
			 if(vis[a[i]&m]>=1&&(a[i]&m)!=a[i])  //第二个条件,怕变的本身
			    {
				flag=1;break;
			    }
			
			if(flag==1)
				 cout<<1<<endl;
			else if(flag1==1)
				cout<<2<<endl;
				else
					cout<<-1<<endl;
		}
		
    }  
    return 0;  
}  

猜你喜欢

转载自blog.csdn.net/sdz20172133/article/details/81711459