CF165E Compatible Numbers(位运算+状压dp)

我们用f[x]表示与x与完得0的数,没有就是-1.考虑一个数x,他可以和~x(x取反)与完得0,即f[~x]=x。而且f[~x的所有子集]=x。所以直接暴力更新的复杂度为O(n·2^22),我们不能接受。。考虑优化,我们每次只更新~x去掉一个1的子集,由大到小递推更新,这样的复杂度为O(2^22·22),就可以过了。

#include <bits/stdc++.h>
#define ll long long
#define pa pair<int,int>
#define N 1000010
int const inf=(1<<22)-1;
using namespace std;
inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x*f;
}
int n,a[N],f[4200000];
int main(){
//  freopen("a.in","r",stdin);
    memset(f,-1,sizeof(f));n=read();
    for(int i=1;i<=n;++i){
        a[i]=read();
        f[(~a[i])&inf]=a[i];
    }
    for(int s=inf;s>=0;--s){
        if(f[s]!=-1) continue;
        for(int j=1;j<=22;++j)
            if(!(s&(1<<j-1))) 
                if((f[s]=f[s|(1<<j-1)])!=-1) break;
    }for(int i=1;i<=n-1;++i) printf("%d ",f[a[i]]);printf("%d\n",f[a[n]]);
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值