题意:给定集合,求一个最大的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的更多相关文章

  1. CF1163E Magical Permutation(线性基,构造)

    虽然做起来有一点裸……但是就是想不到啊…… 首先令 $d_i=p_i\oplus p_{i-1}$,那么 $d_i$ 都是 $S$ 中的数,$a_i=d_i\oplus d_{i-1}\oplus \ ...

  2. CF1163E Magical Permutation【线性基,构造】

    题目描述:输入一个大小为\(n\)的正整数集合\(S\),求最大的\(x\),使得能构造一个\(0\)到\(2^x-1\)的排列\(p\),满足\(p_i\oplus p_{i+1}\in S\) 数 ...

  3. Codeforces 1163E Magical Permutation [线性基,构造]

    codeforces 思路 我顺着图论的标签点进去的,却没想到-- 可以发现排列内每一个数都是集合里的数异或出来的. 考虑答案的上界是多少.如果能用小于\(2^k\)的数构造出\([0,2^k-1]\ ...

  4. Codeforces Round #558 (Div. 2)

    目录 Codeforces Round #558 (Div. 2) 题解 A Eating Soup B Cat Party C Power Transmission D Mysterious Cod ...

  5. Permutation Sequence

    The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  6. [LeetCode] Palindrome Permutation II 回文全排列之二

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  7. [LeetCode] Palindrome Permutation 回文全排列

    Given a string, determine if a permutation of the string could form a palindrome. For example," ...

  8. [LeetCode] Permutation Sequence 序列排序

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  9. [LeetCode] Next Permutation 下一个排列

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

随机推荐

  1. delphi基础篇之单元文件

    Delphi单元文件 unit MainFrm; {库单元文件头} interface        {接口部分由Interface开始implementation结束.声明引用的单元,常量,数据类型 ...

  2. python数据结构之二叉树的统计与转换实例

    python数据结构之二叉树的统计与转换实例 这篇文章主要介绍了python数据结构之二叉树的统计与转换实例,例如统计二叉树的叶子.分支节点,以及二叉树的左右两树互换等,需要的朋友可以参考下 一.获取 ...

  3. 【Android】Retrofit网络请求参数注解,@Path、@Query、@QueryMap.

    对Retrofit已经使用了一点时间了,是时候归纳一下各种网络请求的service了. 下面分为GET.POST.DELETE还有PUT的请求,说明@Path.@Query.@QueryMap.@Bo ...

  4. 基于Netty的RPC架构学习笔记(五):netty线程模型源码分析(二)

    文章目录 小技巧(如何看开源框架的源码) 源码解析 阅读源码技巧 打印查看 通过打断点调试 查看调用栈 小技巧(如何看开源框架的源码) 一断点 二打印 三看调用栈 四搜索 源码解析 //设置nioso ...

  5. ActiveMQ 反序列化漏洞(CVE-2015-5254)

    java -jar jmet-0.1.0-all.jar -Q event -I ActiveMQ -s -Y "touch /tmp/success" -Yp ROME 192. ...

  6. eclipse导入别人项目配置tomcat和jdk

    1.file--import--General--Existing Projiect into Workspace-- 2.导入项目成功后,项目会有错误,需重新进行tomcat及jdk的配置 项目名右 ...

  7. HDU 4325 离散化+树状数组 或者 不使用树状数组

    题意:给出一些花的开放时间段,然后询问某个时间点有几朵花正在开放. 由于ti<1e9,我们需要先将时间离散化,然后将时间点抽象为一个数组中的点,显然,我们需要进行区间更新和单点查询,可以考虑线段 ...

  8. haproxy Mycat集2---KeepAlived

    KA 配两台 MASTER,BACKUP节点 安装Keepalived 1.下载安装依赖包 yum install -y wget make gcc openssl-devel popt-devel ...

  9. 【转】ArcObject与ArcEngine的联系与区别

    在ArcGIS系列产品中,ArcGIS Desktop.ArcGIS Engine和ArcGIS Server都是基于核心组件库ArcObjects搭建的. 所谓ArcObjects,现在一般都是指A ...

  10. batch、随机、Mini-batch梯度下降

    batch梯度下降: 对所有m个训练样本执行一次梯度下降,每一次迭代时间较长: Cost function 总是向减小的方向下降. 随机梯度下降: 对每一个训练样本执行一次梯度下降,但是丢失了向量化带 ...