一、题目

  XOR

二、分析

  给定$N$个数,问它的任意子集异或产生的数进行排列,求第K小的数。

  构造出线性基$B$后,如果$|B| < N$,那么代表N个数中有一个数是可以由线性基中的其他数异或出来的,那么相当于可以异或出$0$。也就是说这种情况下会多一个0作为最小数。

  然后对于第$K$大,可以直接对$K$的二进制进行判断,如果为$1$就取线性基对应的数,然后进行异或就可以得出答案了。

三、AC代码

 1 #include <bits/stdc++.h>
2
3 using namespace std;
4 #define ll long long
5 #define Min(a,b) ((a)>(b)?(b):(a))
6 #define Max(a,b) ((a)>(b)?(a):(b))
7 const int maxn = 1e4 + 13;
8 const int maxl = 62;
9 ll a[maxn];
10
11 struct LinerBasis
12 {
13 vector<ll> vec;
14 int n;
15
16 void build(ll *x, int n)
17 {
18 this->n = n;
19 vector<ll> a(maxl+1);
20 for(int i = 1; i <= n; i++)
21 {
22 ll t = x[i];
23 for(int j = maxl; j >= 0; j--)
24 {
25 if( (t & (1ll<<j)) == 0 )
26 continue;
27 if(a[j])
28 t^=a[j];
29 else
30 {
31 //将低位的已存在的a[k],异或消掉t的这一位1
32 //a[k]=0也没有影响
33 for(int k = 0; k < j; k++)
34 {
35 if(t & (1ll<<k))
36 t ^= a[k];
37 }
38 //消除高位上a[k]的第j位1
39 for(int k = j + 1; k <= maxl; k++)
40 {
41 if(a[k] & (1ll << j))
42 a[k] ^= t;
43 }
44 a[j] = t;
45 break;
46 }
47 }
48 }
49 vec.clear();
50 for(int i = 0; i <= maxl; i++)
51 {
52 if(a[i])
53 vec.push_back(a[i]);
54 }
55 }
56
57 ll query(ll K)
58 {
59 //可能异或出0
60 if(vec.size() < n)
61 K--;
62 if(K > (1ll<<vec.size()) - 1)
63 return -1;
64 ll ans = 0;
65 for(int i = 0; i < vec.size(); i++)
66 {
67 if(K & (1ll << i))
68 {
69 ans ^= vec[i];
70 }
71 }
72 return ans;
73 }
74
75 }lb;
76
77 int main()
78 {
79 int T, Case = 0;
80 scanf("%d", &T);
81 while(T--)
82 {
83 int N, Q;
84 scanf("%d", &N);
85 for(int i = 1; i <= N; i++) scanf("%lld", &a[i]);
86
87 lb.build(a, N);
88
89 scanf("%d", &Q);
90 ll K;
91 printf("Case #%d:\n", ++Case);
92 while(Q--)
93 {
94 scanf("%lld", &K);
95 printf("%lld\n", lb.query(K));
96 }
97 }
98 return 0;
99 }

HDU_3949 XOR 【线性基】的更多相关文章

  1. Xor && 线性基练习

    #include <cstdio> #include <cstring> ; ; int cnt,Ans,b,x,n; inline int Max(int x,int y) ...

  2. 【BZOJ-2115】Xor 线性基 + DFS

    2115: [Wc2011] Xor Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 2142  Solved: 893[Submit][Status] ...

  3. BZOJ.2115.[WC2011]Xor(线性基)

    题目链接 \(Description\) 给定一张无向带边权图(存在自环和重边).求一条1->n的路径,使得路径经过边的权值的Xor和最大.可重复经过点/边,且边权和计算多次. \(Soluti ...

  4. BZOJ2115:[WC2011] Xor(线性基)

    Description Input 第一行包含两个整数N和 M, 表示该无向图中点的数目与边的数目. 接下来M 行描述 M 条边,每行三个整数Si,Ti ,Di,表示 Si 与Ti之间存在 一条权值为 ...

  5. HDU3949 XOR (线性基)

    HDU3949 XOR Problem Description XOR is a kind of bit operator, we define that as follow: for two bin ...

  6. [hdu3949]XOR(线性基求xor第k小)

    题目大意:求xor所有值的第k小,线性基模板题. #include<cstdio> #include<cstring> #include<algorithm> #i ...

  7. HDU3949 XOR(线性基第k小)

    Problem Description XOR is a kind of bit operator, we define that as follow: for two binary base num ...

  8. 2019牛客多校第一场H XOR 线性基模板

    H XOR 题意 给出一组数,求所有满足异或和为0的子集的长度和 分析 n为1e5,所以枚举子集肯定是不可行的,这种时候我们通常要转化成求每一个数的贡献,对于一组数异或和为0.我们考虑使用线性基,对这 ...

  9. BZOJ 2115 [Wc2011] Xor ——线性基

    [题目分析] 显然,一个路径走过两边是不需要计算的,所以我么找到一条1-n的路径,然后向该异或值不断异或简单环即可. 但是找出所有简单环是相当复杂的,我们只需要dfs一遍,找出所有的环路即可,因为所有 ...

  10. BZOJ 2844: albus就是要第一个出场 [高斯消元XOR 线性基]

    2844: albus就是要第一个出场 题意:给定一个n个数的集合S和一个数x,求x在S的$2^n$个子集从小到大的异或和序列中最早出现的位置 一开始看错题了...人家要求的是x第一次出现位置不是第x ...

随机推荐

  1. C++11特性-右值引用

    什么是左值,什么是右值 常见的误区有 = 左边的是左值,右边的是右值. 左值:具有存储性质的对象,即lvalue对象,是指要实际占用内存空间.有内存地址的那些实体对象,例如:变量(variables) ...

  2. PyCharm 中文 字符 python 报错 的 完美 解决方案!

    PyCharm 中文 字符 python 报错 的 完美 解决方案! #_*_ coding:utf-8_*_ https://www.python.org/dev/peps/pep-0263/ 到p ...

  3. 如何在 macOS 上进行滚动截屏

    如何在 macOS 上进行滚动截屏 Shift-Command-5 https://support.apple.com/zh-cn/guide/mac-help/mh26782/mac demo Xn ...

  4. webpack async load modules & dynamic code splitting

    webpack async load modules & dynamic code splitting webpack 按需/异步加载/Code Splitting webpack loade ...

  5. Objec.assign & bug

    Objec.assign & bug shallow copy https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Referenc ...

  6. Error Code: 1452 Cannot add or update a child row: a foreign key constraint fails

    错误: Error Code: 1452 Cannot add or update a child row: a foreign key constraint fails 错误产生情景:我向一张带外键 ...

  7. C++算法代码——纪念品分组[NOIP2007 普及组]

    题目来自:http://218.5.5.242:9018/JudgeOnline/problem.php?id=1099 https://www.luogu.com.cn/problem/P1094 ...

  8. Python学习笔记_有关tuple的几点强调

    创建只有一个元素的tuple,需要用逗号结尾消除歧义 a_tuple = (2,) tuple中的list mixed_tuple = (1, 2, ['a', 'b']) print("m ...

  9. 关于Sidecar Pattern

    本文转载自关于Sidecar Pattern 导语 Sidecar 是一个很纠结的名字,我们在翻译技术雷达时采用了一个比较通俗的翻译,即边车,而这个词随着微服务的火热与 Service Mesh 的逐 ...

  10. Linux文件/proc/net/tcp分析

    本文转载自Linux文件/proc/net/tcp分析 导语 /proc/net/tcp文件提供了tcp的连接信息,是由net/ipv4/tcp_ipv4.c中的tcp4_seq_show()实现信息 ...