题意:给定集合,求一个最大的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个数记下来,格雷码的时候选择是否异或即可。

  1. #include <bits/stdc++.h>
  2.  
  3. const int N = ;
  4.  
  5. int n, a[N], base[], T, id[], sta[N], pos[N], s;
  6.  
  7. inline void clear() {
  8. memset(base, , sizeof(base));
  9. memset(id, , sizeof(id));
  10. return;
  11. }
  12.  
  13. inline void insert(int x, int v) {
  14. int V = ;
  15. for(int i = T - ; i >= ; i--) {
  16. if(!((x >> i) & )) {
  17. continue;
  18. }
  19. if(base[i]) {
  20. x ^= base[i];
  21. V ^= id[i];
  22. }
  23. else {
  24. base[i] = x;
  25. id[i] = V | ( << i);
  26. break;
  27. }
  28. }
  29. return;
  30. }
  31.  
  32. inline bool check() {
  33. for(int i = T - ; i >= ; i--) {
  34. if(!base[i]) {
  35. return false;
  36. }
  37. }
  38. return true;
  39. }
  40.  
  41. inline void find(int x) {
  42. int t = x;
  43. for(int i = T - ; i >= ; i--) {
  44. if(!((x >> i) & )) {
  45. continue;
  46. }
  47. x ^= base[i];
  48. sta[t] ^= id[i];
  49. }
  50. return;
  51. }
  52.  
  53. void DFS(int k) {
  54. if(k == -) {
  55. printf("%d ", pos[s]);
  56. return;
  57. }
  58. DFS(k - );
  59. s ^= << k;
  60. DFS(k - );
  61. return;
  62. }
  63.  
  64. int main() {
  65. scanf("%d", &n);
  66. for(int i = ; i <= n; i++) {
  67. scanf("%d", &a[i]);
  68. }
  69. std::sort(a + , a + n + );
  70. int fin = ;
  71. for(T = ; T <= ; T++) {
  72. int lm = ( << T) - ;
  73. clear();
  74. for(int i = ; i <= n && a[i] <= lm; i++) {
  75. insert(a[i], i);
  76. }
  77. if(check()) {
  78. fin = T;
  79. }
  80. }
  81. T = fin;
  82. /// T
  83. int lm = ( << T) - ;
  84. for(int i = ; i <= lm; i++) {
  85. find(i);
  86. pos[sta[i]] = i;
  87. }
  88. printf("%d\n", T);
  89. DFS(T - );
  90. puts("");
  91. return ;
  92. }

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. Devstack — screen 调试工具的使用

    目录 目录 为什么要使用 screen 工具 启动 screen screen 的切换常用 退出和重新连接 screen Restart Openstack Services screen 指令选项总 ...

  2. Windows系统下安装MySQL 8.0.11数据库

    MySQL数据库是常用的数据库之一,而且该数据库开源免费,所以很多公司在使用.本文记录如何在Windows系统下安装MySQL数据库,本次安装的版本号为8.0.11,这个版本是当前的最新版本,据宣传, ...

  3. kubeadm 安装k8s

    环境要求: 机器名 ip地址 cpu和内存要求 kubernetes-master 10.0.0.11 2c2g(关闭swap) kubernetes-node1 10.0.0.12 2c2g(关闭s ...

  4. docker 错误failed to open stream: Permission denied 解决方法

    在服务器上面.运行docker时,php目录会发生权限问题解决方法如下: 1:进入php目录下面 docker exec -ti php56 /bin/bash #进入php容器 chown -R w ...

  5. PAT L2-021. 点赞狂魔 /// sort+unique去重

    https://www.patest.cn/contests/gplt/L2-021 题目大意: 微博上有个“点赞”功能,你可以为你喜欢的博文点个赞表示支持.每篇博文都有一些刻画其特性的标签,而你点赞 ...

  6. 今天真的很SB

    在公司Review系统网页上,写了一篇几百字的作文, 然后突然手一抖,竟然没有保存就切换页面了, 赶快退回来...没了,啥都没了... 怎么办... 还好洒家N久之前看了一本什么什么杂七杂八的书, 里 ...

  7. flink流的执行大致流程图

  8. vue cli3使用webpack4打包优化

    去掉console.log,以及开启gzip const CompressionPlugin = require('compression-webpack-plugin');//引入gzip压缩插件 ...

  9. adb命令 logcat日志抓取

    一.logcat抓log方法:adb logcat命令,可以加条件过滤 1.安装SDK(参考android sdk环境安装) 2.使用数据线链接手机,在手机助手的sdcard中建立一个1.log的文件 ...

  10. day34 反射、面向对象内置方法:如__str__、面向对象的软件开发

    Python之路,Day21 = 反射.面向对象内置方法:如__str__.面向对象的软件开发 几个内置查看的方法使用 .__base__ 查看类的继承结构.mro() 对象找属性的顺序存在里面 -- ...