原题地址:http://codeforces.com/problemset/problem/165/E
题意:
给定一个序列,要求对于每个序列元素Ai,在序列中寻找另一个元素Aj,使得(Ai & Aj)==0
题解
基于二进制下,对于数Ai,在Ai为1的位上,Aj必然为0,其他位随意
具体见代码
#include<bits/stdc++.h> #define clr(x,y) memset((x),(y),sizeof(x)) using namespace std; typedef long long LL; const int maxn=1e6; const int S=(1<<22)-1; int n; int A[maxn+5]; int dp[S+5]; int main(void) { #ifdef ex freopen ("../in.txt","r",stdin); //freopen ("../out.txt","w",stdout); #endif scanf("%d",&n); for (int i=1;i<=n;++i) { scanf("%d",&A[i]); dp[A[i]^S]=A[i]; } for (int i=S;i>=1;--i) { if (!dp[i]) { for (int j=0;j<=21;++j) { if (dp[i|(1<<j)]) dp[i]=dp[i|(1<<j)]; } } } for (int i=1;i<=n;++i) { if (dp[A[i]]) printf("%d ",dp[A[i]]); else printf("-1 "); } }