Codeforces Round #500 (Div. 2) B. And

题目地址:http://codeforces.com/contest/1013/problem/B

题目:

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.

思路:

给你一个x给你一串数字。用x去和每个数字位运算,看操作几次能让这堆数字里有相同的数字。

假如abcde位运算后变成aacde那就是一次,假如变成effgh,那就是两次。(不可能再有三次),因为x是不变的,无论x和a位运算多少次结果都不会变,1&0=0,1&1=1.

代码:

#include<iostream>
#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)cout<<0<<endl;
        else{
            int t=0,g=0;
            for(int i=0;i<n;i++){
                int k=a[i]&x;
                if(k!=a[i]&&s.count(k)){t=1;break;}
                else if(k!=a[i]&&s1.count(k)){
                    t=2;
                    g=1;
                }
                s1.insert(k);
            }
            if(t==0)cout<<-1<<endl;
            else if(t==1)cout<<1<<endl;
            else cout<<2<<endl;
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Zero_979/article/details/81290251