1013B - And

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个数和一个数x,你可以让n个数与x按位,也可以不操作,得到两个相等的数,所需要的最少步骤。

题解:看了别的博主的思路,和cf的代码,还是python好理解一点。说Python思路吧,c++也差不多,不过处理起来有点麻烦,反正是wa了无数道,python思路,首先对输入的数据去重,如果去重后发现个数比n小,说明有至少相等的2个数,ans=0,然后开个集合(会自动去重的),保存每个数&m的值,如果当前i&m在原来的数里面,说明只需要一次操作,ans=1,最后判断存入的每个数按位&的个数和n比较,小于的ans=2。所以只有0,-1,1,2.四种情况。可能还是不太好理解,按照自己的思路多wa几次,然后看wa样例就能明白好多。

c++:

#include<bits/stdc++.h>
using namespace std;
int n,m,a[100100],b[100100],ans=-1,x;
int main()
{
   cin>>n>>m;
   while(n--)
   {
       cin>>x;
       if(a[x]) ans=0;
       if(ans!=0&&(a[x&m]||b[x])) ans=1;
       if(ans==-1&&b[x&m]) ans=2;
       a[x]++,b[x&m]++;
   }
   cout<<ans<<endl;
    return 0;
}

python:

n,x=map(int,input().split())
a=set(map(int,input().split()))
if len(a)<n: ans=0
else:
    c=set()
    ans=-1
    for i in a:
        c.add(i &x)
        if i&x!=i and i&x in a: ans=1
    if len(c)<len(a) and ans==-1: ans=2
print(ans)

猜你喜欢

转载自blog.csdn.net/memory_qianxiao/article/details/81709862
今日推荐