主题链接~~>

做题情绪:好题,做拉的比赛的时候想了非常久,想到枚举变幻某一位的 0 为 1 。可是每一个数都这样枚举岂不超时的节奏,当时没想到事实上从大到小枚举一次就 ok 了。

解题思路:

本题要求两个数  a & b = 0 , 假设 a  =  10010 , b 至少(指在 a 中的为 1 的位必须为 0 )是 01101 ,还能够是 00101 ,00001 。00000。就相当于你去买东西一样,先提出你的要求(必须满足)。至于其它方面都无所谓。

这样我们能够枚举 b 中的 1 。让其变为 0 ,那么,如何枚举呢  ? 一个一个的枚举是不能够的,肯定超时,我们能够统一枚举一下,就跟状态压缩更新状态一样,相当于递推。用动态规划的思想去优化它,每一个数最多仅仅变化
0 的个数。然后再用变化了的数去变化。

代码:

#include<iostream>
#include<sstream>
#include<map>
#include<cmath>
#include<fstream>
#include<queue>
#include<vector>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<stack>
#include<bitset>
#include<ctime>
#include<string>
#include<cctype>
#include<iomanip>
#include<algorithm>
using namespace std ;
#define INT __int64
#define L(x) (x * 2)
#define R(x) (x * 2 + 1)
const int INF = 0x3f3f3f3f ;
const double esp = 0.0000000001 ;
const double PI = acos(-1.0) ;
const INT mod = 1e9 + 7 ;
const int MY = 15 ;
const int MX = (1<<22) + 5 ;
int n ;
int dp[MX] ,g[MX] ;
int main()
{
//freopen("input.txt" ,"r" ,stdin) ;
while(~scanf("%d" ,&n))
{
int S = (1<<22) - 1 ;
memset(dp ,0 ,sizeof(dp)) ;
for(int i = 0 ;i < n ; ++i)
{
scanf("%d" ,&g[i]) ;
dp[g[i]^S] = g[i] ; // g[I] 须要的还有一半
}
for(int i = S ; i >= 0 ; --i) // 枚举各种状态
{
if(!dp[i]) // 假设没有存值
{
for(int j = 0 ;j < 22 ; ++j) // 给其加入 1 让其变成有值
if(dp[i|(1<<j)])
dp[i] = dp[i|(1<<j)] ;
}
}
for(int i = 0 ;i < n ; ++i)
{
if(i) cout<<" " ;
if(dp[g[i]]) cout<<dp[g[i]] ;
else cout<<"-1" ;
}
cout<<endl ;
}
return 0 ;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

Codeforces 164 E Compatible Numbers的更多相关文章

  1. Codeforces 165 E. Compatible Numbers【子集前缀和】

    LINK 题目大意 给你一个数组,问你数组中的每个数是否可以在数组里面找到一个数和他and起来是0,如果可以就输出这个数,否则就输出-1 思路 首先很显然的是可以考虑找到每个数每一位都取反的数的子集 ...

  2. Codeforces 165E Compatible Numbers(二进制+逆序枚举)

    E. Compatible Numbers time limit per test 4 seconds memory limit per test 256 megabytes input standa ...

  3. [codeforces 55]D. Beautiful numbers

    [codeforces 55]D. Beautiful numbers 试题描述 Volodya is an odd boy and his taste is strange as well. It ...

  4. CodeForces 165E Compatible Numbers(位运算 + 好题)

    wo integers x and y are compatible, if the result of their bitwise "AND" equals zero, that ...

  5. 【Codeforces】CF 165 E Compatible Numbers(状压dp)

    题目 传送门:QWQ 分析 很难想到方向,但有方向了就很easy了. 我们如何减少不必要的计算? 如果我们知道了$ 100111 $的相容的数,$ 100101 $的相容数和他是完全一样的. 我们就靠 ...

  6. CodeForces 164 B. Ancient Berland Hieroglyphs 单调队列

    B. Ancient Berland Hieroglyphs 题目连接: http://codeforces.com/problemset/problem/164/B Descriptionww.co ...

  7. Codeforces 934 A.Compatible Pair

    http://codeforces.com/contest/934 A. A Compatible Pair   time limit per test 1 second memory limit p ...

  8. CodeForces - 1245A Good ol' Numbers Coloring (思维)

    Codeforces Round #597 (Div. 2 Consider the set of all nonnegative integers: 0,1,2,-. Given two integ ...

  9. CodeForces 682A Alyona and Numbers (水题)

    Alyona and Numbers 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/A Description After fi ...

随机推荐

  1. Windows Azure 安全最佳实践 - 第 1 部分:深度解析挑战防御对策

    我每次与开发人员讨论将应用程序迁移到云时都围绕着两个主要问题. 1. 首先是业务.将应用程序迁移到云可以带来怎样的规模经济? 2. 其次是安全问题."云的安全性如何,尤其是Windows A ...

  2. Citrix 服务器虚拟化之三十二 XenConvert

    Citrix 服务器虚拟化之三十二  XenConvert 简介: Citrix XenConvert 是用于实现物理到虚拟(P2V)转换的工具,可将工作负载从运行 Windows 的服务器或桌面计算 ...

  3. JSTL解析——005——core标签库04

    直接入主题,标签讲解 1.<c:import>标签 JSP里面有<% file include="XX"%> 与<jsp:include>,JS ...

  4. Raspberry pi raspbain系统下使用vim

    一开始 apt-get install vim不好用. 在putty中执行这条命令就可以了. sudo apt-get update && sudo apt-get install v ...

  5. U3D——Unity3D的脚本-script入门

     Unity3D的基本操作非常easy就能掌握了,接下来就是游戏系统的核心部分:脚本. 什么是Script(脚本)?简而言之,就是使用代码来运行一系列动作命令的特殊文本,它须要编译器来从新解读.U ...

  6. Ajax - 异步处理(点击变成文本框并修改)

    效果: 对应的文档结构: Test.aspx 前台代码: 引入JQuery(jquery-1.8.3.min.js). 引入自己所写的JS代码(UserJS.js). <html xmlns=& ...

  7. 控制台程序的参数解析类库 CommandLine

    C#控制台程序的参数解析类库 CommandLine简单使用说明 前言 C#开发的控制台程序,默认接收string[] args参数.如果有多个参数需要输入时,可以按照顺序依次输入:但如果有些参数不是 ...

  8. HDU3714 Error Curves (单峰函数)

    大意: 给你n个二次函数Si(x),F(x) = max{Si(x)} 求F(x)在[0,1000]上的最小值. S(x)=ax^2+bx+c       (0<=a<=100, |b|, ...

  9. CocoaPods的install和update卡在“Anylyzing dependencies”的问题解决方式[效率]

    问题 最新CocoaPod更新慢得问题,不管是运行pod install还是podupdate都卡在Anylyzing dependencies. 解决方式 事实上原因是运行两个命令时都会升级Coco ...

  10. 窗体的Alpha通道透明色支持(一旦 Form 被定义为利用 LayeredWindow ,窗口的绘图不再响应沿用多年的 WM_Paint 消息)

    参考: http://www.delphibbs.com/delphibbs/dispq.asp?lid=2190768 Windows 2000后,为了支持类似MAC界面的Alpha通道混合效果,提 ...