LINK


题目大意

给你一个数组,问你数组中的每个数是否可以在数组里面找到一个数和他and起来是0,如果可以就输出这个数,否则就输出-1

思路

首先很显然的是可以考虑找到每个数每一位都取反的数的子集

如果子集中存在一个数就满足,否则就不满足

然后就做一个子集前缀和

for (int i = 0; i < n; i++) {
for (int s = 0; s <= up; s++) {
if ((s >> i) & 1) dp[s] trans from dp[s ^ (1 << i)];
}
}

//Author: dream_maker
#include<bits/stdc++.h>
using namespace std;
//----------------------------------------------
typedef pair<int, int> pi;
typedef long long ll;
typedef double db;
#define fi first
#define se second
#define fu(a, b, c) for (int a = b; a <= c; ++a)
#define fd(a, b, c) for (int a = b; a >= c; --a)
#define fv(a, b) for (int a = 0; a < (signed)b.size(); ++a)
const int INF_of_int = 1e9;
const ll INF_of_ll = 1e18;
template <typename T>
void Read(T &x) {
bool w = 1;x = 0;
char c = getchar();
while (!isdigit(c) && c != '-') c = getchar();
if (c == '-') w = 0, c = getchar();
while (isdigit(c)) {
x = (x<<1) + (x<<3) + c -'0';
c = getchar();
}
if (!w) x = -x;
}
template <typename T>
void Write(T x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9) Write(x / 10);
putchar(x % 10 + '0');
}
//----------------------------------------------
const int N = (1 << 24) + 10;
int n, dp[N], a[N];
int bitlen(int x) {
int res = 0;
while (x) ++res, x >>= 1;
return max(1, res);
}
int main() {
#ifdef dream_maker
freopen("input.txt", "r", stdin);
#endif
memset(dp, -1, sizeof(dp));
Read(n);
int len = 0;
fu(i, 1, n) {
Read(a[i]);
dp[a[i]] = a[i];
len = max(len, bitlen(a[i]));
}
int up = (1 << len) - 1;
fu(i, 0, len)
fu(s, 0, up) if (dp[s] == -1 && ((s >> i) & 1))
dp[s] = dp[s ^ (1 << i)];
fu(i, 1, n) Write(dp[up ^ a[i]]), putchar(' ');
return 0;
}

Codeforces 165 E. Compatible Numbers【子集前缀和】的更多相关文章

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

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

  2. Codeforces 164 E Compatible Numbers

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

  3. CFdiv2 165E. Compatible Numbers 子集枚举

    传送门 题意: 给出一个序列,输出每个数x对应的一个ans,要求ans在数列中,并且ans & x  = 0:数列的每个数小于(4e6) 思路: 这道题的方向比较难想.想到了就比较轻松了,可以 ...

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

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

  5. [codeforces 55]D. Beautiful numbers

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

  6. CodeForces 816B Karen and Coffee(前缀和,大量查询)

    CodeForces 816B Karen and Coffee(前缀和,大量查询) Description Karen, a coffee aficionado, wants to know the ...

  7. 【LeetCode】165. Compare Version Numbers 解题报告(Python)

    [LeetCode]165. Compare Version Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  8. 165. Compare Version Numbers - LeetCode

    Question 165. Compare Version Numbers Solution 题目大意: 比较版本号大小 思路: 根据逗号将版本号字符串转成数组,再比较每个数的大小 Java实现: p ...

  9. Codeforces 449D Jzzhu and Numbers(高维前缀和)

    [题目链接] http://codeforces.com/problemset/problem/449/D [题目大意] 给出一些数字,问其选出一些数字作or为0的方案数有多少 [题解] 题目等价于给 ...

随机推荐

  1. 解决maven的依赖总是无法下载完成

    有时候在eclipse里面刚刚导进一个maven项目,但是总是无法完整下载pom文件里面的依赖 主要有两个原因: 1,需要下载的依赖(jar包)需要开发人员给权限(远程仓库的下载权限),这个可以找开发 ...

  2. c/c++值传递和引用传递

    今天看数据结构的时候,因为是c语言版的,刚开始学的时候就对指针搞的焦头烂额,今天,发现参数传递的时候,&符号也莫名其妙,搜了一篇好文,转载下来. 一. 函数参数传递机制的基本理论 函数参数传递 ...

  3. 454 Authenti cation failed, please open smtp flag first! (Net::SMTPAuthenticationError)

    在用ruby的smtp库发送邮件的时候,出现了这个错误454 Authenti cation failed, please open smtp flag first! (Net::SMTPAuthen ...

  4. sql server 将时间中的时分秒改为00:00:00

    select convert(varchar(10),getdate(),120

  5. python使用set来去重碰到TypeError: unhashable type

    新版:Python 的 unhashable type 错误分析及解决 python使用set来去重是一种常用的方法. 一般使用方法如下: # int a = [1, 2, 3, 4, 5, 1, 2 ...

  6. STM32 IO口双向问题

    源: STM32 IO口双向问题

  7. mongodb的存储引擎

    mongodb版本为3.4 mongodb存储引起的一些概述 存储引擎是MongoDB的核心组件,负责管理数据如何存储在硬盘和内存上.从MongoDB 3.2 版本开始,MongoDB 支持多数据存储 ...

  8. 20145311王亦徐 实验三 "敏捷开发与XP实践"

    20145311王亦徐 实验三 "敏捷开发与XP实践"程序设计过程 实验内容 使用 git 上传代码 使用 git 相互更改代码 实现代码的重载 git 上传代码 查看代码是否有修 ...

  9. git基本配置及使用

    目录 设置git git remote git-flow git merge 与 git rebase 参考 Git是分布式的代码管理工具,远程的代码管理是基于SSH的,所以要使用远程的Git则需要S ...

  10. Memento(备忘录)

    意图: 在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态.这样以后就可将该对象恢复到原先保存的状态. 适用性: 必须保存一个对象在某一个时刻的(部分)状态, 这样以后需要时它 ...