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 ...
随机推荐
- CocoaPods更新2018年11月06日16:06:48
https://gems.ruby-china.org点进去就知道了…… CocoaPods命令 更新 sudo gem install -n /usr/local/bin cocoapods --p ...
- sshpass批量分发ssh秘钥
首先安装sshpass: yum -y install sshpass 单条命令: sshpass -p“password” ssh-copy-id -i /root/.ssh/id_rsa.pub ...
- python学习1-字符串数字基本运算以及if条件和while循环
python学习1-字符串数字基本运算以及if条件和while循环 字符串表达形式共四种: name = "string" name = 'string' name = " ...
- 为kubectl配置别名和命令行补齐
配置别名 # vim ~/.bashrc 添加 alias k='kubectl' # source ~/.bashrc 配置命令行补齐 # yum install -y bash-completio ...
- 2018Github用户kamranahmedse分享的开发路线
下面四张图是Github用户kamranahmedse分享的,主要是web前端开发.后端开发以及DevOps开发的路线图,涉及的点还是很全面的,如果你对这部分有兴趣,并且希望有所作为,以下这几张路线图 ...
- 【学术篇】NOIP2017 d2t3 列队phalanx splay做法
我可去他的吧.... ==============先胡扯些什么的分割线================== 一道NOIP题我调了一晚上...(其实是因为昨晚没有找到调试的好方法来的说...) 曾经我以 ...
- 前端常用的库和实用技术之JavaScript高级技巧
javascript高级技巧 变量作用域和闭包 <!DOCTYPE html> <html lang="en"> <head> <meta ...
- eclipse导出说明文档
选中项目--右键--Export--Java--Javadoc—Finish 1.为程序添加文档注释 2.选中项目--右键Export--Java--Javadoc--next, 3.next--在V ...
- vue 兄弟组件的传值
handleLetterClick方法,采用emit 传递给父组件 父组件触发的方法: handleLetterChange方法: 父组件传递给子组件: CityList组件: 兄弟组件的传值可以 ...
- iptables 命令大全
1.连续端口配置 iptables可以方便的配置多个端口.其中根据端口的连续性,又可分为连续端口配置和不连续端口配置. 如: -A INPUT -p tcp –dport 21:25 -j DROP/ ...