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)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

落凡尘.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值