题目链接:

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. 用不用lambda,这是一个问题

    用不用lambda,这是一个问题  mp.weixin.qq.com Sun在2009年开启了代号为“dolphin”的工程,计划在JDK1.7中加入lambda表达式.虚拟机模块化支持.动态语言支持 ...

  2. Elasticsearch6.5.2 X-pack破解及安装教程

    先正常安装 elasticSearch, kibana. 1. 如果是6.5.2版本,可以直接下载jar文件:https://download.csdn.net/download/bigben0123 ...

  3. django系列8:优化vote页面,使用通用视图降低代码冗余

    修改detail.html,将它变为一个可用的投票页面 <h1>{{ question.question_text }}</h1> {% if error_message %} ...

  4. selenium技术博客

    1.java+selenium+详细的api说明和seleniumGrid使用,缺点是代码不够好看. http://www.cnblogs.com/yytesting/p/5714175.html

  5. (二叉树 递归) leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal

    Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  6. 数据结构Java实现03----栈:顺序栈和链式堆栈

    一.堆栈的基本概念: 堆栈(也简称作栈)是一种特殊的线性表,堆栈的数据元素以及数据元素间的逻辑关系和线性表完全相同,其差别是线性表允许在任意位置进行插入和删除操作,而堆栈只允许在固定一端进行插入和删除 ...

  7. mysql MHA架构搭建过程

    [环境介绍] 系统环境:Red Hat Enterprise Linux 7 + 5.7.18 + MHA version 0.57 系统 IP 主机名 备注 版本 xx系统 192.168.142. ...

  8. POJ 2318 TOYS (叉积+二分)

    题目: Description Calculate the number of toys that land in each bin of a partitioned toy box. Mom and ...

  9. CAD版本知识

  10. Android屏幕旋转

    一个手机最基本的旋转方向有上面4种,而在Android开发中,涉及到屏幕旋转的地方很多,而且各个函数给出的角度值都不一样,比如 Activity的getRotate,Camera的setDisplay ...