CF1013B And
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
4 3
1 2 3 7
1
2 228
1 1
0
3 7
1 2 3
-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(n≤100000) 的序列 ai(ai≤100000),并给定一个数 x(x≤100000) 。
每一步可以将序列中的一个数与上 x。
求使序列中出现两个相等的数的最小步数。如果不可能则输出 −1。
思路:给出两个数 n 与 x,以及长度为 n 的一个数组 a[n],现在问数组中是否有相同的元素,如果有输出0,如果没有则进行 a[i] & x 操作,然后与原数组相比,如果有相同的元素,就输出1,如果还是没有,就看是否存在一个数操作完后与这个数相等,如果存在就输出2,否则,输出-1。
#include <stdio.h>
#include <algorithm>
#include <iostream>
using namespace std;
int a[200005],b[200005];
int main()
{
int i,j,k,n,x,s=-1;
cin>>n>>x;
while(n--){
cin>>k;
if(a[k]!=0){
s=0;
}
if(s!=0 && (a[k&x]!=0||b[k]!=0)){
s=1;
}
if(s==-1 && b[k&x]){
s=2;
}
a[k]++;
b[k&x]++;
}
cout<<s<<endl;
return 0;
}
CF1013B And的更多相关文章
- 【题解】CF1013B And
题面传送门 解决思路 首先我们可以得出,$ a $ \(\&\) $ x $ \(=\) $ a $ \(\&\) $ x $ \(\&\) $ x $.由此得知,同一个 \( ...
- 题解合集 (update on 11.5)
收录已发布的题解 按发布时间排序. 部分可能与我的其他文章有重复捏 qwq . AtCoder for Chinese: Link ZHOJ: Link 洛谷 \(1\sim 5\) : [题解]CF ...
随机推荐
- 编写高质量的Python代码系列(四)之元类及属性
元类(metaclass)及动态属性(dynamic attribute)都是很强大的Python特性,然后他们也可能导致及其古怪.及其突然的行为.本节讲解这些机制的常见用法,以确保各位程序员写出来的 ...
- Entity Framework系列教程汇总
翻译自http://www.entityframeworktutorial.net/,使用EF几年时间了,一直没有系统总结过,所以翻译这一系统文章作为总结,由于英语功底有限,翻译的可能有些问题,欢迎指 ...
- 当前标识没有对“C:\WINDOWS\Microsoft.NET\...”的写访问权限的解决办法
1.需要重新注册IIS服务扩展,在开始运行中输入以下命令运行:aspnet_regiis -i 32位的Windows: --------------------------------------- ...
- 半导体制造、Fab以及Silicon Processing的基本知识
本文转载自微信公众号 - 手机技术资讯 , 链接 https://mp.weixin.qq.com/s/602xLKXcIw4ccTnhvDP1xw
- 课程设计个人报告——基于ARM实验箱的捕鱼游戏的设计与实现
课程设计个人报告--基于ARM实验箱的捕鱼游戏的设计与实现 一.个人贡献 参与课设题目讨论及部分过程 资料收集 负责代码调试 捕鱼游戏相应功能的实现 实验环境 Eclipse软件开发环境: ARM实验 ...
- python和Java的几种容器比较
python里面有几种常用的容器用来存放数据 1.list 列表,在python中没有数据类型,所以可以在列表中存放各种类型的数据,比如把Java里面的整型和字符串放在一个容器里面如下所示 list ...
- Java(18) 集合框架
一.集合框架 Collectoin Map List set HashMap ArrayList LinkedList ...
- 【转载】PyTorch系列 (二):pytorch数据读取
原文:https://likewind.top/2019/02/01/Pytorch-dataprocess/ Pytorch系列: PyTorch系列(一) - PyTorch使用总览 PyTorc ...
- [JLOI2014]松鼠的新家-树链剖分
最开始的时候我在写线段树部分的时候还打了一个build,后来一想,打个球球大作战的build啊!!!有个锤子的用啊!!! #include<bits/stdc++.h> using nam ...
- java - day008 -final ,static ,访问控制符.
面向对象 封装 类: 模板 对象: 实例 引用,遥控器 构造方法 新建对象时执行 ...