【codeforces 165E】 - Compatible Numbers 【位运算】
2016-01-13 20:10
330 查看
E. Compatible Numbers
time limit per test4 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Two integers x and y are compatible, if the result of their bitwise “AND” equals zero, that is, a & b = 0. For example, numbers 90 (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 test(s)
input
2
90 36
output
36 90
input
4
3 6 3 6
output
-1 -1 -1 -1
input
5
10 6 9 8 2
output
-1 8 2 2 8
題意:数列找a&b=0
题解: 对于范围内的数1–(1<<22)-1用DP预处理:
1.先求反
2.1—->0 is possible
time limit per test4 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Two integers x and y are compatible, if the result of their bitwise “AND” equals zero, that is, a & b = 0. For example, numbers 90 (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 test(s)
input
2
90 36
output
36 90
input
4
3 6 3 6
output
-1 -1 -1 -1
input
5
10 6 9 8 2
output
-1 8 2 2 8
題意:数列找a&b=0
题解: 对于范围内的数1–(1<<22)-1用DP预处理:
1.先求反
2.1—->0 is possible
#include<iostream> #include<stdio.h> using namespace std; const int N=(1<<22)-1; int n,a[10000005]; int dp[N+1]; int main() { scanf("%d",&n); for(int i=1;i<=n;i++) //to guarantee the existence of dp[i] { scanf("%d",&a[i]); dp[N^a[i]]=a[i]; } for(int i=N;i>=0;i--) { if(!dp[i]) //1---->0 is possible for(int j=0;j<22;j++) { if(dp[i|(1<<j)]) { dp[i]=dp[i|(1<<j)]; } } } for(int i=1;i<=n;i++) { if(i-1!=0) cout<<" "; if(dp[a[i]]) { cout<<dp[a[i]]; } else cout<<-1; } }
相关文章推荐
- java 解析xml字符串
- Android一些经验技巧(持续更新)
- 再说CSS3渐变——线性渐变
- 面试题12:打印1到最大的n位数
- 三种java 去掉字符串中的重复字符函数
- 文件只能安装一次
- 软件测试_BadBoy自动化测试工具10_导出脚本用于JMeter性能测试
- Android中的Selector的用法
- java递归算法,例子从1到100的整数和。
- 2_1、OSI参考模型和TCP/IP协议族
- 【UE4学习】01——UE4下载与安装
- 液晶屏LVDS,TTL,RSDS接口样式的区别方法
- 内核等待队列
- 处理重复问题
- valuestack,stackContext,ActionContext.之间的关系
- Java 数据结构之List去重
- lcd fb参数如何计算
- Opencv学习之cvCreateMat的用法
- ulimit详解
- [系统与控制]力场盾系统升级原因