Codeforces 165E Compatible Numbers
来源:互联网 发布:淘宝卖蜂蜜需要什么 编辑:程序博客网 时间:2024/05/20 07:53
题目链接:http://codeforces.com/problemset/problem/165/E
题意:给出n大小的数列,为数列中的每一个数找到一个与其进行&操作后等于0的数,并且该数也在该数列中
思路:假设一个数的二进制为10101,将其0位取反得到一些数:11101,10111,11111,与变化后的数进&操作得到0的数同样&1010等于0,那么将数列中所有数按照这样方法进行预处理,出现过的数用vis数组标记为1,如果我们要判断一个数x能否在该数列中找到&操作后等于0的数,我们就将x取反得到y,如果vis[y]等于1,那么就可以找到
接下来就要将y复原成数列中的数,我们尝试将y中的1取反得到yc,如果vis[yc]依然等于1,那么该位置可以不必要等于1,我们将y更新yc;如果vis[yc]等于0,那么这一位必然等于1,res+=(1<<i)。
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#define maxn 4000000using namespace std;int s[1000030],vis[5000030];int getlen(int x){ int sum=0; while (x) { x/=2; sum++; } return sum;}int main(){ int n; while (scanf("%d",&n)!=EOF) { int maxx=0; memset(vis,0,sizeof(vis)); for (int i=0;i<n;i++) { scanf("%d",&s[i]); vis[s[i]]=1; maxx=max(s[i],maxx); } int len=getlen(maxx); maxx=(1<<(len))-1; for (int i=0;i<maxx;i++) { if (vis[i]) { for (int j=0;j<len;j++) { if ((i&(1<<j))==0) { vis[i|(1<<j)]=1; } } } } for (int i=0;i<n;i++) { int tmp=maxx-s[i]; if (!vis[tmp]) { printf("%d ",-1); } else { int res=0; for (int j=0;j<len;j++) { if (tmp&(1<<j)) { int tmp2=tmp^(1<<j); if (!vis[tmp2]) res+=(1<<j); else tmp^=(1<<j); } } printf("%d ",res); } } printf("\n"); } return 0;}
0 0
- codeforces 165E - Compatible Numbers
- CodeForces 165E Compatible Numbers
- Codeforces 165E Compatible Numbers
- Codeforces 165E Compatible Numbers
- codeforces 165E - Compatible Numbers 【位运算】
- 【codeforces 165E】 - Compatible Numbers 【位运算】
- Codeforces 165E Compatible Numbers 题解&代码
- Codeforces 165E Compatible Numbers 【dp】
- CodeForces 165E Compatible Numbers (dp)
- Codeforces 164 E Compatible Numbers
- CodeForces Round #112 Div2 165 E. Compatible Numbers
- codeforces 165E Compatible Numbers(位运算)【模板】
- 状态压缩DP codeforces 244 Problem C. The Brand New Function 和 codeforces 165 E. Compatible Numbers
- CodeForces 478E Wavy numbers
- CodeForces 215E Periodical Numbers 数位DP
- Codeforces 746E Numbers Exchange 模拟
- Codeforces Round #FF (Div. 2) E.DZY Loves Fibonacci Numbers
- 【Codeforces Round #FF (Div. 2)】E. DZY Loves Fibonacci Numbers
- 在windows上通过浏览器远程连接Linux服务器的jupyter
- HDU2521 反素数
- Android面试——编程算法题
- CGroup 介绍、应用实例及原理描述
- [C++]最小生成树--Prim算法&Kruskal算法
- Codeforces 165E Compatible Numbers
- 利用labview动态生成树
- Max Time-Delay Neural Networks
- 算法课实验
- 腐蚀和膨胀
- Android基础——广播接收者BroadcastReceiver
- android学习之路---java部分 I/O流
- AndroidManifest.xml配置文件详解
- 【Java】多线程实现方式