Codeforces 1013B And(模拟) 题解

题目来源:

http://codeforces.com/problemset/problem/1013B

题目描述:

 

题目描述

There is an array with nn elements a_{1},a_{2},...,a_{n}a1​,a2​,...,an​ and the number xx .

In one operation you can select some ii ( 1<=i<=n1<=i<=n ) and replace element a_{i}ai​ with $ a_{i}&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≠ji≠j such that a_{i}=a_{j}ai​=aj​ . Determine whether it is possible to achieve and, if possible, the minimal number of operations to apply.

输入输出格式

输入格式:

 

The first line contains integers nn and xx ( 2<=n<=1000002<=n<=100000 , 1<=x<=1000001<=x<=100000 ), number of elements in the array and the number to and with.

The second line contains nn integers a_{i}ai​ ( 1<=a_{i}<=1000001<=ai​<=100000 ), the elements of the array.

 

输出格式:

 

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

 

输入输出样例

输入样例#1: 复制

4 3
1 2 3 7

输出样例#1: 复制

1

输入样例#2: 复制

2 228
1 1

输出样例#2: 复制

0

输入样例#3: 复制

3 7
1 2 3

输出样例#3: 复制

-1

说明

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,问最少经过几次操作,可以是这串数字存在两个以上相同,可能为0

解题思路:

      一开始想了半天,后来发现a^x^x=a^x,所以答案只可以是1,2,0,-1,这样,我们就只要模拟的扫两遍数组就行。。代码:

#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <iomanip>
using namespace std;
int jl[100005],su[100005],vis[100005];
int main()
{
    int n,x,flag=0,tt=0;
    cin>>n>>x;
    for(int i=1;i<=n;i++)
    {
        cin>>su[i];
        jl[su[i]]++;
        if(jl[su[i]]>1){flag=1;break;}
    }
    if(flag)cout<<0<<endl;
    else{
        for(int i=1;i<=n;i++)
        {
            int t=su[i]&x;
            if(jl[t]&&su[i]!=t){flag=1;break;}
            vis[t]++;
            if(vis[t]>1){tt=1;}
        }
        if(flag)cout<<"1"<<endl;
        else if(tt)cout<<"2"<<endl;
        else cout<<"-1"<<endl;
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值