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 ...
随机推荐
- 代码实现跟控制器跳转到storyBoard
- python文件操作 seek(),tell()
seek():移动文件读取指针到指定位置 tell():返回文件读取指针的位置 seek()的三种模式: (1)f.seek(p,0) 移动当文件第p个字节处,绝对位置 (2)f.seek(p,1) ...
- 【转】Pro Android学习笔记(三六):Fragment(1):基本概念
目录(?)[-] 为何引入Fragment 大小屏幕的适配 横屏竖屏切换 返回键 什么是Fragment 为何引入Fragment 我们之前的Activity都是都是全屏处理较为简单的单一事务功能,适 ...
- window下rails4.1 发生TZInfo::DataSourceNotFound 错误 - smallbottle
在官网上学习rails 4.1 ,启动rails server之后发生了如下错误 $ rails server Booting WEBrick Rails 4.1.0 application star ...
- 11_adb指令练习
通过adb指令咱们装相关的项目.把项目推到设备上.也可以进行文件相关的操作.adb的一些相关的指令. 开启连接IDE和设备的服务. adb可以安装应用也可以卸载应用.项目怎么去区分?一个包名一个是签名 ...
- Servlet编程实例 续3
----------------siwuxie095 Servlet 跳转之请求的转发 修改 LoginServlet.java: package com.siwuxie095.servlet; im ...
- 树莓派 Learning 003 --- GPIO 001 --- 点亮LED
树莓派 Learning 003 - GPIO 001 - 点亮LED 我的树莓派型号:Raspberry Pi 2 Model B V1.1 装机系统:NOOBS v1.9.2 树莓派 Learni ...
- unreal3控制台窗口属性调整
在windows平台上,unreal3的console窗口类是FOutputDeviceConsoleWindows 启动时,它可以从XXXGame.ini中读取诸如窗口大小之类的属性,具体的代码在 ...
- 8.Struts2-057漏洞复现
漏洞信息: 定义XML配置时如果namespace值未设置且上层动作配置(Action Configuration)中未设置或用通配符namespace时可能会导致远程代码执行. url标签未设置va ...
- MapperScan的工作,Spring-Mybatis怎么自动getMapper
@MapperScan @Import(MapperScannerRegistrar.class) @Repeatable(MapperScans.class) public @interface M ...