CF1163E Magical Permutation
题意:给定集合,求一个最大的x,使得存在一个0 ~ 2x - 1的排列,满足每相邻的两个数的异或值都在S中出现过。Si <= 2e5
解:若有a,b,c,令S1 = a ^ b, S2 = b ^ c,则有a ^ c = S1 ^ S2
因为有0存在,所以每个数都能表示成它到0路径上的所有间隔的异或和,也就是每个数都能被表示出来。我们用线性基来判断。
找到最大的x之后,我们可以发现,线性基中x个数的2x种选法一一对应这2x个数。于是直接采用格雷码来找这个排列,每加一位,就在x个数中添加 / 删除一个数到异或集合中。
我的实现较复杂。实际上只要把线性基这x个数记下来,格雷码的时候选择是否异或即可。
#include <bits/stdc++.h>
const int N = ;
int n, a[N], base[], T, id[], sta[N], pos[N], s;
inline void clear() {
memset(base, , sizeof(base));
memset(id, , sizeof(id));
return;
}
inline void insert(int x, int v) {
int V = ;
for(int i = T - ; i >= ; i--) {
if(!((x >> i) & )) {
continue;
}
if(base[i]) {
x ^= base[i];
V ^= id[i];
}
else {
base[i] = x;
id[i] = V | ( << i);
break;
}
}
return;
}
inline bool check() {
for(int i = T - ; i >= ; i--) {
if(!base[i]) {
return false;
}
}
return true;
}
inline void find(int x) {
int t = x;
for(int i = T - ; i >= ; i--) {
if(!((x >> i) & )) {
continue;
}
x ^= base[i];
sta[t] ^= id[i];
}
return;
}
void DFS(int k) {
if(k == -) {
printf("%d ", pos[s]);
return;
}
DFS(k - );
s ^= << k;
DFS(k - );
return;
}
int main() {
scanf("%d", &n);
for(int i = ; i <= n; i++) {
scanf("%d", &a[i]);
}
std::sort(a + , a + n + );
int fin = ;
for(T = ; T <= ; T++) {
int lm = ( << T) - ;
clear();
for(int i = ; i <= n && a[i] <= lm; i++) {
insert(a[i], i);
}
if(check()) {
fin = T;
}
}
T = fin;
/// T
int lm = ( << T) - ;
for(int i = ; i <= lm; i++) {
find(i);
pos[sta[i]] = i;
}
printf("%d\n", T);
DFS(T - );
puts("");
return ;
}
AC代码
CF1163E Magical Permutation的更多相关文章
- CF1163E Magical Permutation(线性基,构造)
虽然做起来有一点裸……但是就是想不到啊…… 首先令 $d_i=p_i\oplus p_{i-1}$,那么 $d_i$ 都是 $S$ 中的数,$a_i=d_i\oplus d_{i-1}\oplus \ ...
- CF1163E Magical Permutation【线性基,构造】
题目描述:输入一个大小为\(n\)的正整数集合\(S\),求最大的\(x\),使得能构造一个\(0\)到\(2^x-1\)的排列\(p\),满足\(p_i\oplus p_{i+1}\in S\) 数 ...
- Codeforces 1163E Magical Permutation [线性基,构造]
codeforces 思路 我顺着图论的标签点进去的,却没想到-- 可以发现排列内每一个数都是集合里的数异或出来的. 考虑答案的上界是多少.如果能用小于\(2^k\)的数构造出\([0,2^k-1]\ ...
- Codeforces Round #558 (Div. 2)
目录 Codeforces Round #558 (Div. 2) 题解 A Eating Soup B Cat Party C Power Transmission D Mysterious Cod ...
- Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [LeetCode] Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. For example," ...
- [LeetCode] Permutation Sequence 序列排序
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
随机推荐
- sudo apt-get update:Could not get lock /var/lib/apt/lists/lock解决办法
原文: http://blog.chinaunix.net/uid-26932153-id-3193335.html 今天更新时候出现了点小问题,一开始更新到一半,我嫌速度慢,就取消掉了. 更新了so ...
- Metasploit 模块和位置
Metasploit Framework由许多的模块组成的. 一.Exploits(漏洞模块) 定义为使用“有效载荷(payloads)”的模块 没有“有效载荷”的攻击是辅助模块 二.Payloads ...
- 根据单个或多个字段对list对象去重
pojo 省略 在list 对象中,根据某一字段进行去重,重写Comparator /** * 去重 * * @param orderList * @return * @author ziggo * ...
- leetcode题解(持续更新)
leetcode题解 1.两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但 ...
- (一)hello word
1.在如图demo文件夹下可以编写自己的后端代码. 我们编写hello的接口代码如下: @RestController @RequestMapping("/hello") @Slf ...
- 9款很棒的网页绘制图表JavaScript框架脚本
推荐9款很棒的可在网页中绘制图表的JavaScript脚本,这些有趣的JS脚本可以帮助你快速方便的绘制图表(线.面.饼.条…),其中包括jQuery.MooTools.Prototype和一些其它的J ...
- Linux服务器下对Oracle数据库expdp(导出)和impdp(导入)
紧接上篇文章,Oracle数据库架构已经创建完成,我的需求是:将老服务器上的数据库迁移到新的数据库上. 这就用到impdp(导入)操作. 要想实现对新数据库的impdp(导入)工作, 首先需要从老的数 ...
- 使用mapreduce对日志进行清洗
网站日志分析项目案例(一)项目介绍:http://www.cnblogs.com/edisonchou/p/4449082.html 网站日志分析项目案例(二)数据清洗:当前页面 网站日志分析项目案例 ...
- JS对象 字符串分割 split() 方法将字符串分割为字符串数组,并返回此数组。 语法: stringObject.split(separator,limit)
字符串分割split() 知识讲解: split() 方法将字符串分割为字符串数组,并返回此数组. 语法: stringObject.split(separator,limit) 参数说明: 注意:如 ...
- Java类加载器浅述
jdk默认提供了三种类加载器: 1.Bootstrap ClassLoader(引导类加载器): 将<JAVA_HOME>\lib目录下的类库加载到虚拟机内存中,用来加载java的核心库, ...