CodeForces 165E Compatible Numbers (dp)
来源:互联网 发布:荣威rx5 知乎 编辑:程序博客网 时间:2024/06/07 06:05
题意:给你n个数,让你找到一个任意a[j]使得 a[j] & a[i] == 0,不存在输出-1。
思路:我们可以知道a[i]&(M^a[i])一定为0,(M为二进制全为1),当然a[i]不仅仅只和M^a[i]与运算后为0,M^a[i]的二进制中还有许多位为1,那么这些变为0,也是可以作为答案的。所以可以dp递推处理,见代码。
代码:
#include<bits/stdc++.h>using namespace std;const int maxn = 1e7+5;int n, dp[maxn], a[maxn];int main(void){ while(cin >> n) { memset(dp, 0, sizeof(dp)); int M = (1<<23)-1; for(int i = 1; i <= n; i++) { scanf("%d", &a[i]); dp[M^a[i]] = a[i]; } for(int i = M; i >= 0; i--) { if(!dp[i]) { for(int j = 0; j < 23; 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"); if(i == n) puts(""); else printf(" "); } } return 0;}
阅读全文
1 0
- Codeforces 165E Compatible Numbers 【dp】
- CodeForces 165E Compatible Numbers (dp)
- 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 164 E Compatible Numbers
- 状态压缩DP codeforces 244 Problem C. The Brand New Function 和 codeforces 165 E. Compatible Numbers
- CodeForces Round #112 Div2 165 E. Compatible Numbers
- codeforces 165E Compatible Numbers(位运算)【模板】
- CodeForces 215E Periodical Numbers 数位DP
- CodeForces 165E 【状压DP】
- CodeForces 478E Wavy numbers
- codeforces 165E DP 数论 二进制
- 【dp】codeforces 83E
- maven profile配置
- nginx openresty异常处理小记
- Codeforces Round #430 (Div. 2)
- C++ operator关键字(重载操作符)
- 【C#】解决那些参数不同的方法无法注册委托的情况
- CodeForces 165E Compatible Numbers (dp)
- 欢迎使用CSDN-markdown编辑器
- 图形与图像编程(一)-图形编程
- mybatis xml 映射文件 sql include 的用法
- Foxmail 收取邮件提示失败
- OpenCV在VS中的配置
- HBITMAP和CBitmap的互相转换
- MySQL 第三天
- 【CSP201409-3】字符串匹配