hdu3949(线性基,求第k小的异或和
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3949
XOR
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4731 Accepted Submission(s): 1658
For each test case, the first line is an integer N(1<=N<=10000), the number of numbers below. The second line contains N integers (each number is between 1 and 10^18). The third line is a number Q(1<=Q<=10000), the number of queries. The fourth line contains Q numbers(each number is between 1 and 10^18) K1,K2,......KQ.
2
1 2
4
1 2 3 4
3
1 2 3
5
1 2 3 4 5
1
2
3
-1
Case #2:
0
1
2
3
-1
If you choose a single number, the result you get is the number you choose.
Using long long instead of int because of the result may exceed 2^31-1.
#include<iostream>
#include<string.h>
#include<map>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<cmath>
#include<ctype.h>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=1e9+;
const int maxn=1e4+;
const int maxk=5e3+;
const int maxx=1e4+;
const ll maxe=+;
#define INF 0x3f3f3f3f3f3f
#define Lson l,mid,rt<<1
#define Rson mid+1,r,rt<<1|1
ll a[maxn],b[];
ll m,k;
void guass(int n)
{
memset(b,,sizeof(b));
for(int i=;i<n;i++)
{
for(int j=;j>=;j--)
{
if((a[i]>>j)&)
{
if(!b[j])
{
b[j]=a[i];
break;
}
else
{
a[i]^=b[j];
}
}
}
}
for(int i=;i>=;i--)
{
if(!b[i]) continue;
for(int j=i+;j<=;j++)
{
if((b[j]>>i)&) b[j]^=b[i];//为了使得只有b[i]的第i为1,其它的都不为1
}
}
m=;
for(int i=;i<=;i++) if(b[i]) b[m++]=b[i];
}
int main()
{
int t,ca=,n,q;
cin>>t;
while(t--)
{
cin>>n;
printf("Case #%d:\n",ca++);
for(int i=;i<n;i++)
{
cin>>a[i];
}
guass(n);
cin>>q;
while(q--)
{
ll ans=;
cin>>k;
if(n!=m) k--;//代表可以是0
if(k>=(1ll<<m)) cout<<"-1"<<endl;
else
{
for(int i=;i<=;i++) if((k>>i)&) ans^=b[i];
cout<<ans<<endl;
}
}
}
return ;
}
hdu3949(线性基,求第k小的异或和的更多相关文章
- 线性基求第k小异或值
题目链接 题意:给由 n 个数组成的一个可重集 S,每次给定一个数 k,求一个集合 \(T \subseteq S\), 使得集合 T 在 S 的所有非空子集的不同的异或和中, 其异或和 \(T_1 ...
- 算法导论学习之线性时间求第k小元素+堆思想求前k大元素
对于曾经,假设要我求第k小元素.或者是求前k大元素,我可能会将元素先排序,然后就直接求出来了,可是如今有了更好的思路. 一.线性时间内求第k小元素 这个算法又是一个基于分治思想的算法. 其详细的分治思 ...
- 树状数组求第k小的元素
int find_kth(int k) { int ans = 0,cnt = 0; for (int i = 20;i >= 0;i--) //这里的20适当的取值,与MAX_VAL有关,一般 ...
- hdu 4217 Data Structure? 树状数组求第K小
Data Structure? Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- 线性基求交(2019牛客国庆集训派对day4)
题意:https://ac.nowcoder.com/acm/contest/1109/C 问你有几个x满足A,B集合都能XOR出x. 思路: 就是线性基求交后,有几个基就是2^几次方. #defin ...
- 求第k小的数
题目链接:第k个数 题意:求n个数中第k小的数 题解: //由快速排序算法演变而来的快速选择算法 #include<iostream> using namespace std; const ...
- [hdu3949]XOR(线性基求xor第k小)
题目大意:求xor所有值的第k小,线性基模板题. #include<cstdio> #include<cstring> #include<algorithm> #i ...
- UVA11525 Permutation[康托展开 树状数组求第k小值]
UVA - 11525 Permutation 题意:输出1~n的所有排列,字典序大小第∑k1Si∗(K−i)!个 学了好多知识 1.康托展开 X=a[n]*(n-1)!+a[n-1]*(n-2)!+ ...
- *HDU2852 树状数组(求第K小的数)
KiKi's K-Number Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
随机推荐
- Python之常用模块(二)
shelve xml处理 configparser hashlib logging shelve模块 shelve是一个简单的k,v将内存数据通过文件持久化的模块,可以持久化任何pickle可支持 ...
- 人物-IT-张志东:张志东
ylbtech-人物-IT-张志东:张志东 张志东,广东东莞人,腾讯创办人之一,腾讯高级副总裁兼科技总裁,于1993年取得深圳大学理学学士学位,并于1996年取得华南理工大学计算机应用及系统架构硕士学 ...
- C#事件触发机制
C#的事件触发机制,类似于c++的回调函数机制 我先简单说一下,委托和事件的实质,后期再重开一篇博文来详细说 委托:指向方法的指针,类似于C的函数指针 事件:是一个可以存放0个或多个方法指针的数据结构 ...
- [cf839d]Winter is here容斥原理
题意:给定一个数列${a_i}$,若子序列长度为$k$,最大公约数为$gcd$,定义子序列的权值为$k*\gcd (\gcd > 1)$.求所有子序列的权值和. 答案对10^9+7取模. 解题 ...
- JavaScript的内部对象
JavaScript的内部对象 按创建方式不同分为:使用变量声明的隐性对象,使用new创建的显性对象 隐性对象 在赋值和声明后就是一个隐性对象,隐性对象不支持prototype属性,也无法随意扩展对象 ...
- 向PCD文件写入点云数据
博客转载自:http://www.pclcn.org/study/shownews.php?lang=cn&id=83 本小节我们学习如何向PCD文件写入点云数据. 代码 章例2文件夹中,打开 ...
- LightOJ 1311 Unlucky Bird (物理题)
题意:有点长,意思是说有一个鸟,在两列火车之间不停的来回飞,两列相距为d时,都开始减速,直到最后停止下来,正好是相距0米, 现在给定两列车的速度和减速时的加速度,和鸟的速度求 d 和鸟飞过的路程. 析 ...
- htmlparser API
htmlparser所有的filter htmlparser所有的Tags htmlparser API: http://htmlparser.sourceforge.net/javadoc/inde ...
- Spark 各个组件关系
Term Meaning Application User program built on Spark. Consists of a driver program and executors on ...
- 序列化 (C#)
序列化是指将对象转换成字节流,从而存储对象或将对象传输到内存.数据库或文件的过程. 它的主要用途是保存对象的状态,以便能够在需要时重新创建对象. 反向过程称为"反序列化". 序列化 ...