题目链接:

https://vjudge.net/problem/1735275/origin

基本思路:

本题思路比较简单,首先,我们知道 a & x = b, b & x = b; 所以,一个数通过与运算改变只能改变一次!

所以,这里就有一种暴力的写法,三次for循环,时间复杂度是O(3n)。

第一次,从1到n遍历一遍,计算各个元素出现的次数,如果大于等于2,则输出0,结束。

第二次,从1到n遍历,如果存在 vis[ a[i]&k ] >= 2 并且 与k与运算后的值与之前的不同,即a[i] != a[i]&k,于是输出1,结束。

第三次,从1到n遍历,对a[i]&k的元素加一,即vis[ a[i]&k ]++,如果存在vis[ a[i]&k ] >= 2 && (a[i]&k) != a[i],则输出2,结束。

AC代码如下:

#include <iostream>
#include <cstdio> using namespace std;
const int MX = 1e5+;
int a[MX], vis[MX]; int main()
{
int n, k;
scanf("%d%d", &n, &k);
for(int i = ; i <= n; ++i) scanf("%d", &a[i]);
for(int i = ; i <= n; ++i)
{
vis[ a[i] ]++; // 第一次统计一下元素的个数
if(vis[ a[i] ] >= )
{
printf("0\n");
return ;
}
}
for(int i = ; i <= n; ++i)
{
// 一次与运算有值并且这个与运算得到的结果和之前的值不同时则说明操作一次即可
if(vis[ a[i]&k ] == && a[i] != (a[i]&k)) // 注意要加括号!
{
printf("1\n");
return ;
}
}
for(int i = ; i <= n; ++i)
{
vis[ a[i]&k ]++; // 操作两次时直接遍历一遍
if(vis[ a[i]&k ] >= && a[i] != (a[i]&k)) // 这里要注意一下与运算重复的现象!
{
printf("2\n");
return ;
}
}
printf("-1\n");
return ;
}

第二种解法用到了flag标记。

第一种情况,未进行与运算就存在,则输出0。

第二种情况, 进行了一次与运算与之前元素重复,或者输入元素与之前与运算元素重复,则与对比1求最小值。

第三种情况, 与运算结果与之前与运算结果相同则与2对比得最小值。

下面是AC代码:

#include <iostream>
#include <cstdio> using namespace std;
const int INF = 0x3f3f3f3f;
const int MX = 1e5+;
bool flag[MX][]; // 一个数有两种状态! int main()
{
int ans = INF;
int n, k;
scanf("%d%d", &n, &k);
for(int i = ; i <= n; ++i)
{
int x;
scanf("%d", &x);
int y = x&k;
if(flag[x][]) // 未进行与运算就存在,则输出0
{
ans = min(ans, );
}
else if(flag[y][] || flag[x][]) //进行了一次与运算与之前元素重复,或者输入元素与之前与运算元素重复,则与对比1求最小值
{
ans = min(ans, );
}
else if(flag[y][]) // 与运算结果与之前与运算结果相同则与2对比得最小值
{
ans = min(ans, );
}
flag[x][] = true;
flag[y][] = true;
}
if(ans == INF) printf("-1\n"); // 都不符合作则输出-1
else printf("%d\n", ans);
return ;
}

如有疑问,欢迎评论指出!

CodeForces - 1013B And 与运算暴力的更多相关文章

  1. Codeforces 868D Huge Strings - 位运算 - 暴力

    You are given n strings s1, s2, ..., sn consisting of characters 0 and 1. m operations are performed ...

  2. Codeforces Gym 100015H Hidden Code 暴力

    Hidden Code 题目连接: http://codeforces.com/gym/100015/attachments Description It's time to put your hac ...

  3. Codeforces gym 100685 A. Ariel 暴力

    A. ArielTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685/problem/A Desc ...

  4. Codeforces Gym 100637G G. #TheDress 暴力

    G. #TheDress Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100637/problem/G ...

  5. [ An Ac a Day ^_^ ] CodeForces 691F Couple Cover 花式暴力

    Couple Cover Time Limit: 3000MS   Memory Limit: 524288KB   64bit IO Format: %I64d & %I64u Descri ...

  6. Codeforces 626D Jerry's Protest(暴力枚举+概率)

    D. Jerry's Protest time limit per test:2 seconds memory limit per test:256 megabytes input:standard ...

  7. codeforces 650D D. Image Preview (暴力+二分+dp)

    题目链接: http://codeforces.com/contest/651/problem/D D. Image Preview time limit per test 1 second memo ...

  8. Codeforces Gym 100203G Good elements 暴力乱搞

    原题链接:http://codeforces.com/gym/100203/attachments/download/1702/statements.pdf 题解 考虑暴力的复杂度是O(n^3),所以 ...

  9. Codeforces 839D Winter is here - 暴力 - 容斥原理

    Winter is here at the North and the White Walkers are close. John Snow has an army consisting of n s ...

随机推荐

  1. iOS开发者知识普及,Swift 挑战 Objective-C,谁会笑到最后?

    前言: 目前全球共有超过 7 亿台 iPhone 处于活跃状态,全球约有2000万名 iOS 开发者,这造就了 iOS 作为全球第二大移动设备平台的状态. 虽然安卓系统的全球市场占有率超过 iOS 系 ...

  2. 金融量化分析【day110】:NumPy-切片和索引

    一.索引和切片 1.数组和标量之间的运算 2.同样大小的数组之间的运算 3.数组索引 4.数组切片 1.一维数组 2.多维数组 二.布尔索引 1.问题 给一个数组,选出数组中所有大于5的数 1.答案 ...

  3. Hadoop记录- Yarn scheduler队列采集

    #!/bin/sh ip=10.116.100.11 port=8088 export HADOOP_HOME=/app/hadoop/bin rmstate1=$($HADOOP_HOME/yarn ...

  4. sublime中编译的sass如何改变css输出风格?【这里有答案】

    由于在网上找了一遍没找到如果在sublime中将sass编译的css转换成为自己喜欢的风格,所以换了一种思路搜索到了答案,这里我将讲述如果更改. 首先sass总共有四种编译风格 (1) nested( ...

  5. JN_0002:Win10禁止U盘拷贝文件的方法

    1,在电脑桌面使用快捷键win键+r唤出运行窗口,在搜索框中输入gpedit.msc,然后点击确定. 2,打开的本地组策略编辑器中依次点击展开计算机配置—管理模块—系统,在系统下找到并选中可移动存储访 ...

  6. ArcGis 属性表.dbf文件使用Excel打开中文乱码的解决方法

    2019年4月 拓展: ArcGis——好好的属性表,咋就乱码了呢? 2019年3月27日补充: 在ArcMap10.3+(根据官网描述应该是,作者测试使用10.5,可行)以后的版本,可以使用ArcT ...

  7. 边框回归(bounding-Box regression)

    转自:https://blog.csdn.net/zijin0802034/article/details/77685438 为什么要边框回归? 什么是边框回归? 边框回归怎么做的? 边框回归为什么宽 ...

  8. Database学习 - mysql 视图/触发器/函数

  9. [Kubernetes]如何让集群为我们工作?

    前一段时间倒腾k8s的时候,写了一系列的博客,有很多人不理解那样做的意义是什么,为什么要那样做,这篇文章就尝试解释一下,在实际环境中,是如何让集群为我们工作的. 因为只研究了一个月左右的时间,认识难免 ...

  10. Idea 创建控制台程序

    1:前提配置好 jdk环境. 最后一步:填写项目名称和保存的路径 即可.