B. And

本文探讨了通过位运算(尤其是按位与操作)来优化数组中元素的匹配过程,旨在使数组至少包含两个相等的元素。文章详细介绍了算法的实现思路,包括如何利用按位与操作修改数组元素,以及如何通过最少的操作次数达到目标状态。代码示例展示了如何通过迭代检查数组元素,并使用计数策略确定达到目标所需的最小操作数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 andoperation.

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.

代码:

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    int n,x;
    scanf("%d%d",&n,&x);
    int a[100005];
    int b[100005];
    fill(a,a+100005,0);
    fill(b,b+100005,0);
    for(int i=0;i<n;++i)
    {
        int p,q;
        scanf("%d",&p);
        q=p&x;
        if(p==q) a[p]++;
        else{
            a[p]++;b[q]++;
        }
    }
    for(int i=0;i<100005;++i)
    {
        if(a[i]>=2) { printf("0\n");return 0;}
    }
    for(int i=0;i<100005;++i)
    {
        if(a[i]==1&&b[i]>=1){printf("1\n");return 0;}
    }
    for(int i=0;i<100005;++i)
    {
        if(b[i]>=2){printf("2\n");return 0;}
    }
    printf("-1\n");
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值