0
点赞
收藏
分享

微信扫一扫

codeforces 165E Compatible Numbers


​​http://www.elijahqi.win/archives/1219​​​
E. Compatible Numbers
time limit per test

4 seconds
memory limit per test

256 megabytes
input

standard input
output

standard 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.
Examples
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

题意:题目给出一列数 要求在这一列数中为每个数找一个和它&之后为0的答案 并输出
题目提供spj

那么我们知道比如十进制800=二进制1100100000

和他&为0的值一定只有每一位0的地方可以是1

那么那个数最大是11011111 好了 假如把这个数任何一位1删去它和800&运算的时候仍然是0

那么我们每次只需要记录下来这个数取反的这个数可以是我们当前这个数如果每次都去更新它的子集 显然复杂度是无法接受的 那么先记录下来,相当于给所以先都打标记,最后一起下放

然后从最大的111…此处大约21位1 然后开始向下枚举

每次如果这个数有答案则向下更新

#include<cstdio>
#include<algorithm>
#define N 4400000
#define N1 1100000
using namespace std;
inline char gc(){
static char now[1<<16],*S,*T;
if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}
return *S++;
}
int n,a[N1],flag1[N],flag[N],f[N];
inline int read(){
int x=0;char ch=gc();
while (ch<'0'||ch>'9') ch=gc();
while (ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=gc();}
return x;
}
int main(){
freopen("cf.in","r",stdin);
n=read();int bin=1<<22;
for (int i=1;i<=n;++i) {
int cnt=0,tmp=0;a[i]=read();tmp=a[i];
while (tmp){cnt++;tmp>>=1;}
f[(bin-1)^a[i]]=a[i];
}
for (int s=(1<<22)-1;s>=0;--s){
if (f[s]){
int cnt=0,tmp=s;while(tmp){cnt++;tmp>>=1;}
for (int j=0;j<=cnt-1;++j){
if ((s>>j)&1){
int s1=s-(1<<j);
f[s1]=f[s];
}
}
}
}
for (int i=1;i<=n;++i) if (f[a[i]]==0) printf("-1\n");else printf("%d\n",f[a[i]]);
return 0;
}


举报

相关推荐

0 条评论