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;
}

猜你喜欢

转载自blog.csdn.net/qq_40400202/article/details/81431918