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. NPOI+反射+自定义特性实现上传excel转List及验证

    1.自定义特性 [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)] public ...

  2. VS2010/MFC编程入门之六(对话框:创建对话框模板和修改对话框属性)

    鸡啄米在上一讲中介绍了MFC的消息映射机制,属于原理方面的知识.对于VC++编程入门学习者来说可能有些抽象,鸡啄米会把消息映射的知识渗透到后面的教程中.本节开始为大家讲解偏应用的知识-创建对话框. 对 ...

  3. GOEXIF读取和写入EXIF信息

    最新版本的gexif,直接基于gdi+实现了exif信息的读取和写入,代码更清晰. /* * File: gexif.h * Purpose: cpp EXIF reader * 3/2/2017 & ...

  4. ZLYD团队第三周项目总结

    ZLYD团队第三周项目总结 项目进展 我们的吃豆子游戏的程序由八个文件组成:Wall.java.Gold.java.Player.java.Fruit.java.Enemy.java.Ticker.j ...

  5. Git合并分支或者冲突

     假设冲突文件是 test/TestCase.php  下面分5种情况讨论. 1.本地不变.   然后远程别人有更新.   git pull   这种最简单,没有冲突,本地工作区直接更新   2.我本 ...

  6. Java FastJson 介绍

    1.前言 1.1.FastJson的介绍: JSON协议使用方便,越来越流行,JSON的处理器有很多,这里我介绍一下FastJson,FastJson是阿里的开源框架,被不少企业使用,是一个极其优秀的 ...

  7. HDU 1712 ACboy needs your help(分组背包入门题)

    http://acm.hdu.edu.cn/showproblem.php?pid=1712 题意: 有个人学习n门课程,a[i][j]表示用j分钟学习第i门课程所能获得的价值,背包容量为一共有m时间 ...

  8. Solidity 官方文档中文版 1_简介

    简介 Solidity是一种语法类似JavaScript的高级语言.它被设计成以编译的方式生成以太坊虚拟机代码.在后续内容中你将会发现,使用它很容易创建用于投票.众筹.封闭拍卖.多重签名钱包等等的合约 ...

  9. 请问使用jmeter在tcp取样器测试中服务器名称或ip,端口可以填变量值吗?

    请问使用jmeter在tcp取样器测试中服务器名称或ip,端口可以填变量值吗?

  10. python 列表元素替换以及删除

    >>> letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] >>> letters ['a', 'b', 'c', 'd', ...