CodeForces 165E Compatible Numbers(位运算 + 好题)
wo integers x and y are compatible, if the result of their bitwise "AND" equals zero, that is, a&b = 0. For example, numbers90(10110102) and 36(1001002) are compatible, as 10110102&1001002 = 02, and numbers 3(112) and 6(1102) are not compatible, as 112&1102 = 102.
You are given an array of integers a1, a2, ..., an. Your task is to find the following for each array element: is this element compatible with some other element from the given array? If the answer to this question is positive, then you also should find any suitable element.
Input
The first line contains an integer n (1 ≤ n ≤ 106) — the number of elements in the given array. The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 4·106) — the elements of the given array. The numbers in the array can coincide.
Output
Print n integers ansi. If ai isn't compatible with any other element of the given array a1, a2, ..., an, then ansi should be equal to -1. Otherwise ansi is any such number, that ai&ansi = 0, and also ansi occurs in the array a1, a2, ..., an.
Sample Input
2
90 36
36 90
4
3 6 3 6
-1 -1 -1 -1
5
10 6 9 8 2
-1 8 2 2 8
题意:给出 N 个数,在这个 N 个数中,如果存在 与 它 与操作为 0 的则输出那个数,否则输出-1;
分析: 对于一个数 53 (1 1 0 1 0 1) 先取反 得 ( 0 0 1 0 1 0)那么 与 53 与操作 为 0 的数 必定是 取反之后 1 位置的数 取0 或者取1都可以的。
一大神这样做的: dp[x] = y;表示 x 与 y 与 后为0, 那么 dp[x ^ Max] = x; 跟最大的数(全是1) 异或之后 就相当于取反,也就相当于 53 (1 1 0 1 0 1) 先取反 得 ( 0 0 1 0 1 0),然后就可以枚举每一个1的位置的值了, 从最大的开始 Max ,如果 dp[x]为0 那么就每一位枚举 看看 x 能否通过 将 某一位变成 1 之后 是不为 0 的;
也就是对于 53 来说 从 1 1 1 1 1 1 开始枚举,看看 是否某一位变成 1 就 可以和 0 0 1 0 1 0 一样了,然后得到 0 0 0 0 1 0和 0 0 1 0 0 0(此时这两个值已经存在),然后一值往下 找 改变一个值看看 是否跟存在的值一样
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int Max = ( << ) - ; // 让 21 为全都1
int dp[Max];
int a[Max];
int main()
{
int n;
scanf("%d", &n);
memset(dp, , sizeof(dp));
for (int i = ; i < n; i++)
{
scanf("%d", &a[i]);
dp[ a[i] ^ Max ] = a[i]; // 对每一数取反
}
for (int i = Max; i >= ; i--)
{
if (!dp[i]) // 如果不是存在,看看是否能通过改变某一位
{
for (int j = ; j < ; j++) //枚举
{
if (dp[i|( << j)])
dp[i] = dp[i | ( << j)];
}
}
}
for (int i = ; i < n - ; i++)
{
if (dp[a[i]])
printf("%d ", dp[a[i]]);
else
printf("-1 ");
}
if (dp[a[n - ]])
printf("%d\n", dp[a[n - ]]);
else
printf("-1\n"); return ;
}
CodeForces 165E Compatible Numbers(位运算 + 好题)的更多相关文章
- Codeforces 165E Compatible Numbers(二进制+逆序枚举)
E. Compatible Numbers time limit per test 4 seconds memory limit per test 256 megabytes input standa ...
- 蓝桥杯---汉字取首字母(位运算 & 水题)
确实题目虽然有点水,但是开始的时候好像还真的没有想到怎么提取出这个编号一不小心感觉可以可以用unsigned char 这种类型,直接转为16进制,但是之后发现虽然第一次在codeblock中还行,但 ...
- Codeforces 868D Huge Strings - 位运算 - 暴力
You are given n strings s1, s2, ..., sn consisting of characters 0 and 1. m operations are performed ...
- 【洛谷4424】[HNOI/AHOI2018] 寻宝游戏(位运算思维题)
点此看题面 大致题意: 给你\(n\)个\(m\)位二进制数.每组询问给你一个\(m\)位二进制数,要求你从\(0\)开始,依次对于这\(n\)个数进行\(and\)或\(or\)操作,问有多少种方案 ...
- 【BZOJ4300】绝世好题(位运算水题)
点此看题面 大致题意: 给你一个序列\(a\),让你求出最长的一个子序列\(b\)满足\(b_i\&b_{i-1}!=0\). 位运算+\(DP\) 考虑设\(f_i\)表示以第\(i\)个数 ...
- codeforces - 15C Industrial Nim(位运算+尼姆博弈)
C. Industrial Nim time limit per test 2 seconds memory limit per test 64 megabytes input standard in ...
- zoj--3870--Team Formation(位运算好题)
Team Formation Time Limit: 3000MS Memory Limit: 131072KB 64bit IO Format: %lld & %llu Submit ...
- zzulioj--1832--贪吃的松鼠(位运算好题)
1832: 贪吃的松鼠 Time Limit: 3 Sec Memory Limit: 2 MB Submit: 43 Solved: 7 SubmitStatusWeb Board Descri ...
- Codeforces 878A - Short Program(位运算)
原题链接:http://codeforces.com/problemset/problem/878/A 题意:给出n个位运算操作, 化简这些操作, 使化简后的操作次数不多于5步. 思路:我们可以对二进 ...
随机推荐
- MVP ComCamp & GCR MVP Openday 2014
今年的MVP Openday与往年不一样,加入了Community Camp环节,即社区大课堂.其主要形式是由MVP作为讲师提供包括Developer和IT Pro方向的课程,地点是在北京国际会议中心 ...
- 查看Linux版本信息
如何查看Linux系统使用的版本信息呢? 下面这篇文章收集.整理了一些常见的查看Linux系统版本的方法.由于手头只有Oracle Linux.Centos Linux.Redhat Linux三个版 ...
- php之验证码小程序
验证码功能(个人理解): 减轻服务器的压力(如12306的验证码功能): 防止暴力注册 个人思路:在a-z,A-Z,1-9生成n位随机的数来构成新的验证码. 关于生成验证码的几个小函数 range() ...
- 深入解析SQL Server并行执行原理及实践(下)
谈完并行执行的原理,咱们再来谈谈优化,到底并行执行能给我们带来哪些好处,我们又应该注意什么呢,下面展开. Amdahl’s Law 再谈并行优化前我想有必要谈谈阿姆达尔定律,可惜老爷子去年已经驾鹤先 ...
- percona教程:MySQL GROUP_CONCAT的使用
percona有一篇blog: The power of MySQL GROUP_CONCAT 比较详细地介绍了GROUP_CONCAT函数的用法.简单地翻译了一下. 假设你有4名工程师,这周他们为6 ...
- S3C6410移植apache和php
需要准备的东西: Apache-1.3.39 for linux Php-4.4.8 for linux Ubuntu amd64位 PC机 6410开发板,我用的是友善之臂 交叉编译: 交叉编译呢, ...
- 整理几篇比较好的AndroidUI动画开发文章
第一篇,写的比较详细,新手可以看得懂: http://www.360doc.com/content/16/0128/08/30422106_531162539.shtml
- 【小白的CFD之旅】09 初识FLUENT
按黄师姐的推荐,小白回头查找起 FLUENT的资料来.通过网络及图书馆查找相关资料,小白对于FLUENT有了基本的认识. FLUENT是一个CFD软件包,目前隶属于ANSYS公司 目前FLUENT的最 ...
- TFS修改项目名称
引言 如何让TFS管理的项目更名后不丢失修改历史记录? 在工作中有时由于前期考虑不足,某个Project的名字在项目开发过程中需要修改以更准备表达它的功能.本文就对此进行抛砖引玉 操作 在Source ...
- [WPF系列]-基础系列 TabControl应用
引言 Tabcontrol控件也是我们在项目中经常用到的一个控件,用它将相关的信息组织在一起分类显示. 简介 ========================================= ...