CodeForces Round #112 Div2 165 E. Compatible Numbers

來源:互聯網
上載者:User
/*經典DP一定一個數組a1,a2,a3.....對於每一個ai在此數組中找到一個數aj,使得ai&aj=0,如果沒有這樣的aj,輸出-1;n最大1000000.對於一個ai,按位取反後的數x,肯定滿足ai&x=0;但aj=x是充分不必要條件,對與x,它的二進位中若干個1變為0並不影響ai&x=0;所以只要數組中存在這類x中的一個就行了。*/#include <iostream>#include <algorithm>#include <map>#include <set>#include <vector>#include<cstdio>#include<cstring>using namespace std;int a[1000009];int hash[10000009];int k;int main() {    while(scanf("%d",&k)!=EOF)    {        memset(hash,-1,sizeof(hash));        int L=1<<22;       for(int i=1;i<=k;i++)       {       scanf("%d",&a[i]);       hash[a[i]]=a[i];       }       for(int i=1;i<L;i++)       if(hash[i]>0)       {            for(int j=0;j<22;j++)            if(hash[i|(1<<j)]==-1)//找到i|(1<<j)可以通過那個ai通過某幾位由0變而來            {                hash[i|(1<<j)]=hash[i];            }       }       for(int i=1;i<=k;i++)       {            int t=(L-1)^a[i];            printf("%d ",hash[t]);            puts("");        }    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.