Codeforces 165 E. Compatible Numbers【子集前缀和】
题目大意
给你一个数组,问你数组中的每个数是否可以在数组里面找到一个数和他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【子集前缀和】的更多相关文章
- 【Codeforces】CF 165 E Compatible Numbers(状压dp)
题目 传送门:QWQ 分析 很难想到方向,但有方向了就很easy了. 我们如何减少不必要的计算? 如果我们知道了$ 100111 $的相容的数,$ 100101 $的相容数和他是完全一样的. 我们就靠 ...
- Codeforces 164 E Compatible Numbers
主题链接~~> 做题情绪:好题,做拉的比赛的时候想了非常久,想到枚举变幻某一位的 0 为 1 .可是每一个数都这样枚举岂不超时的节奏,当时没想到事实上从大到小枚举一次就 ok 了. 解题思路: ...
- CFdiv2 165E. Compatible Numbers 子集枚举
传送门 题意: 给出一个序列,输出每个数x对应的一个ans,要求ans在数列中,并且ans & x = 0:数列的每个数小于(4e6) 思路: 这道题的方向比较难想.想到了就比较轻松了,可以 ...
- Codeforces 165E Compatible Numbers(二进制+逆序枚举)
E. Compatible Numbers time limit per test 4 seconds memory limit per test 256 megabytes input standa ...
- [codeforces 55]D. Beautiful numbers
[codeforces 55]D. Beautiful numbers 试题描述 Volodya is an odd boy and his taste is strange as well. It ...
- CodeForces 816B Karen and Coffee(前缀和,大量查询)
CodeForces 816B Karen and Coffee(前缀和,大量查询) Description Karen, a coffee aficionado, wants to know the ...
- 【LeetCode】165. Compare Version Numbers 解题报告(Python)
[LeetCode]165. Compare Version Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 165. Compare Version Numbers - LeetCode
Question 165. Compare Version Numbers Solution 题目大意: 比较版本号大小 思路: 根据逗号将版本号字符串转成数组,再比较每个数的大小 Java实现: p ...
- Codeforces 449D Jzzhu and Numbers(高维前缀和)
[题目链接] http://codeforces.com/problemset/problem/449/D [题目大意] 给出一些数字,问其选出一些数字作or为0的方案数有多少 [题解] 题目等价于给 ...
随机推荐
- 线性表:实现单链表和子类栈(Stack)及单向队列(Queue) [C++]
刚刚开始学习c++.之前c的内容掌握的也不多,基本只是一本概论课的程度,以前使用c的struct写过的链表.用python写过简单的数据结构,就试着把两者用c++写出来,也是对c++的class,以及 ...
- Java Int和Integer有什么区别?
Int int是我们常说的整型数字,是Java的8个原始数据类型(Primitive Type:boolean.byte.short.char.int.float.double.long)之一.Jav ...
- Android 创建SQLite数据库(一)
Android内置了轻量级的数据库SQLite,这里将自己理解作个记录,方便自己复习. 一.首先,创建SQLite数据库比较常见的方式是通过Android提供的SQLiteOpenHelper来实现, ...
- Apache配置WSGI
Apache配置WSGI 什么是WSGI WSGI被称作web服务器网关接口,在笔者看来其实是基于CGI标准针对Python语言做了一些改进,其主要功能是规范了web 服务器与Pythonj应用程序之 ...
- 通过J2EE Web工程添加Flex项目,进行BlazeDS开发
http://www.cnblogs.com/noam/archive/2010/07/22/1782955.html 环境:Eclipse 7.5 + Flex Builder 4 plugin f ...
- ubuntu常用命令解释
1.seq 用于产生一个整数到另一个整数之间所有的整数,如:seq 3 5 输出:3 4 5 注:如果参数为一个整数,则输出1至这个数之间的所有整数 2.tee [-a] file 从标准输入设备读取 ...
- Linux及安全实践二
Linux及安全实践二 基本内核模块 20135238 龚睿 1. 理解模块原理 linux模块是一些可以作为独立程序来编译的函数和数据类型的集合.之所以提供模块机制,是因为Linux本身是一个 ...
- 关于spring框架工作原理的初解
一:spring基本概念 1)struts2是web框架,hibernate是orm框架 2)spring是容器框架,创建bean,维护bean之间的关系 3)spring可以管理web层,持久层,业 ...
- Gym 101243E Cupcakes
http://codeforces.com/gym/101243/attachments 题意: 有n个人,桌子上有k的蛋糕,每个人都有一个值val,表示每次轮到他吃蛋糕时,他可以吃1~val的蛋糕量 ...
- python 列表元素替换以及删除
>>> letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] >>> letters ['a', 'b', 'c', 'd', ...