zoukankan      html  css  js  c++  java
  • codeforce 165E Compatible Numbers

    原题地址: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 ");
        }
    }
  • 相关阅读:
    3.26
    3.25
    3.24
    3.23 JS学习
    3.22 团队作业1 疫情数据可视化
    3.19 个人作业1源码
    3.18 个人作业源码
    5.06python
    5.05Android
    5.04Android
  • 原文地址:https://www.cnblogs.com/123-123/p/5845829.html
Copyright © 2011-2022 走看看