主题链接~~>

做题情绪:好题,做拉的比赛的时候想了非常久,想到枚举变幻某一位的 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. CLR和.Net对象

    CLR和.Net对象生存周期 前言 1. 基础概念明晰* 1.1 公告语言运行时* 1.2 托管模块* 1.3 对象和类型* 1.4 垃圾回收器 2. 垃圾回收模型* 2.1 为什么需要垃圾回收* 2 ...

  2. Qt入门-字符串类QString

    原地址:http://blog.csdn.net/xgbing/article/details/7770854 QString是Unicode字符的集合,它是Qt API中使用的字符串类. QStri ...

  3. C# 它 抽象类和接口

    抽象类 C#同意把类和方法声明为abstract,即抽象类和抽象方法.抽象类通常代表一个抽象概念,它提供一个继承的出发点,当设计一个新的对象类时,一定是用来继承的,所以,在一个以继承关系形成的等级结构 ...

  4. Effective C++_笔记_条款12_复制对象时勿忘其每一个成分

    (整理自Effctive C++,转载请注明.整理者:华科小涛@http://www.cnblogs.com/hust-ghtao/) 编译器会在必要时候为我们的classes创建copying函数, ...

  5. 把linux可执行程序做成一个服务[转]

    转自:http://www.2cto.com/os/201202/121249.html 在linux系统启动的时候,我们可以看到很多服务性程序一个接一个的被启动(就是那些后面有一个兰色[OK]的行) ...

  6. Mybatis 数据库物理分页插件 PageHelper

    以前使用ibatis/mybatis,都是自己手写sql语句进行物理分页,虽然稍微有点麻烦,但是都习惯了.最近试用了下mybatis的分页插件 PageHelper,感觉还不错吧.记录下其使用方法. ...

  7. iText 文本

    iText中用文本块(Chunk).短语(Phrase)和段落(paragraph)处理文本. 文本块(Chunk)是处理文本的最小单位,有一串带格式(包括字体.颜色.大小)的字符串组成.如以下代码就 ...

  8. ExtJs4 笔记(11) Ext.ListView、Ext.view.View 数据视图

    本篇介绍两个用来展示数据的容器控件,分别是Ext.ListView和Ext.view.View.Ext.ListView就是大名鼎鼎的Ext GridPanel的前身,不过现在的Ext4已经将它整合到 ...

  9. 序列化TList of objects(摘自danieleteti的网站)

    Some weeks ago a customer asked to me if it is possibile serialize a TList of objects. “Hey, you sho ...

  10. 数独问题的介绍及POJ 2676-Sudoku(dfs+剪枝)

    知道是数独问题后犹豫了一下要不要做(好像很难的样纸==.),用dfs并剪枝,是一道挺规范的搜索题. 先介绍以下数独吧- 数独(Sudoku)是一种运用纸.笔进行演算的逻辑游戏.玩家需要根据9×9盘面上 ...