CodeForces 165E 【状压DP】

来源:互联网 发布:统计软件spss下载 编辑:程序博客网 时间:2024/06/07 01:35
 



E. Compatible Numbers

Two integers x and y are compatible, if the result of their bitwise "AND" equals zero, that is,a&b = 0. For example, numbers90(10110102) and36(1001002) are compatible, as10110102&1001002 = 02, and numbers3(112) and6(1102) are not compatible, as112&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 containsn 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 arraya1, a2, ..., an, thenansi should be equal to -1. Otherwiseansi is any such number, thatai&ansi = 0, and alsoansi occurs in the arraya1, a2, ..., an.

Examples
Input
290 36
Output
36 90
Input
43 6 3 6
Output
-1 -1 -1 -1
Input
510 6 9 8 2
Output
-1 8 2 2 8

题意:n个正整数,对于每一个ai,是否存在aj&ai=0,若存在则输出aj否则输出-1;
            脑洞题,dp[N^a[i]]=a[i];(N=1<<22-1) 往下顺推,具体看代码。
#include<cstring>#include<cstdio>#include<iostream>#include<algorithm>using namespace std;int dp[1<<22],a[1001000],c[25],n;int main(){    c[0]=1;    for(int i=1;i<=23;i++)        c[i]=c[i-1]<<1;    while(scanf("%d",&n)!=EOF)    {        for(int i=1;i<=n;i++)        {            scanf("%d",&a[i]);            dp[a[i]]=-1;        }        for(int i=1;i<=n;i++)        dp[(c[22]-1)^a[i]]=a[i];        for(int i=c[22]-1;i>0;i--)        if(dp[i]>0)        {            for(int j=0;j<22;j++)                if(i&c[j])            {                dp[i-c[j]]=dp[i];            }        }        for(int i=1;i<n;i++)            printf("%d ",dp[a[i]]);        printf("%d\n",dp[a[n]]);    }    return 0;}



原创粉丝点击