您的位置:首页 > 其它

【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

#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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: