【HDU3949】XOR
【题目大意】
给定一个数组,求这些数组通过异或能得到的数中的第k小是多少。
传送门:http://vjudge.net/problem/HDU-3949
【题解】
首先高斯消元求出线性基,然后将k按照二进制拆分即可。
注意当高斯消元结束后若末尾有0则第1小是0 特判一下然后k--。
然后HDU输出long long是用%I64d 无论C++还是G++都是。(虽然我用了lld也AC了)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<ctime>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
#define MAXN 10010
ll T,n,m,flag,a[MAXN];
inline ll read()
{
ll x=,f=; char ch=getchar();
while(!isdigit(ch)) {if(ch=='-') f=-; ch=getchar();}
while(isdigit(ch)) {x=x*+ch-''; ch=getchar();}
return x*f;
}
void guess()
{
ll temp=;
for(ll i=(1ll<<),j;i;i>>=)
{
for(j=temp+;j<=n;j++) if(a[j]&i) break;
if(j>n) continue;
swap(a[++temp],a[j]);
for(ll j=;j<=n;j++) if(j!=temp&&(a[j]&i)) a[j]^=a[temp];
}
flag=(temp!=n);
n=temp;
}
ll ask(ll x)
{
x-=flag; ll ans=;
if(!x) return ;
for(int i=n;i;i--) {if(x&) ans^=a[i]; x>>=;}
if(x) return -;
return ans;
}
int main()
{
//freopen("cin.in","r",stdin);
//freopen("cout.out","w",stdout);
T=read();
for(int CASE=;CASE<=T;CASE++)
{
printf("Case #%d:\n",CASE);
n=read();
for(int i=;i<=n;i++) a[i]=read();
guess();
m=read();
for(int i=;i<=m;i++) {ll x=read(); printf("%I64d\n",ask(x));}
}
return ;
}
附上makedata程序:
#include<iostream>
#include<cstdio>
#include<ctime>
#include<cstdlib>
using namespace std;
int main()
{
freopen("cin.in","w",stdout);
srand(time(NULL));
int T=;
printf("%d\n",T);
while(T--)
{
int n=; printf("%d\n",n);
for(int i=;i<=n;i++) printf("%I64d ",(long long)rand()*rand()*rand());
int m=; printf("\n%d\n",m);
for(int i=;i<=m;i++) printf("%d ",rand()%n+);
printf("\n");
}
return ;
}
【HDU3949】XOR的更多相关文章
- 【BZOJ2337】Xor和路径(高斯消元)
[BZOJ2337]Xor和路径(高斯消元) 题面 BZOJ 题解 我应该多学点套路: 对于xor之类的位运算,要想到每一位拆开算贡献 所以,对于每一位拆开来看 好了,既然是按位来算 我们就只需要计算 ...
- 【BZOJ2115】Xor(线性基)
[BZOJ2115]Xor(线性基) 题面 BZOJ Description Input 第一行包含两个整数N和 M, 表示该无向图中点的数目与边的数目. 接下来M 行描述 M 条边,每行三个整数Si ...
- BZOJ 2115 【Wc2011】 Xor
Description Input 第一行包含两个整数N和 M, 表示该无向图中点的数目与边的数目. 接下来M 行描述 M 条边,每行三个整数Si,Ti ,Di,表示 Si 与Ti之间存在 一条权值为 ...
- 【BZOJ-2115】Xor 线性基 + DFS
2115: [Wc2011] Xor Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 2142 Solved: 893[Submit][Status] ...
- 【bzoj2115】 Xor
www.lydsy.com/JudgeOnline/problem.php?id=2115 (题目链接) 题意 给出一张图,可能有重边和自环,在图中找出一条从1-n的路径,使得经过的路径的权值的异或和 ...
- 【bzoj2115】【wc2011】Xor
2115: [Wc2011] Xor Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 5380 Solved: 2249[Submit][Status ...
- bzoj2115【WC2001】Xor
2115: [Wc2011] Xor Time Limit: 10 Sec Memory Limit: 259 MB Submit: 2059 Solved: 856 [Submit][Statu ...
- 【整理】XOR:从陌生到头晕
一:解决XOR常用的方法: 在vjudge上面输入关键词xor,然后按照顺序刷了一些题. 然后大概悟出了一些的的套路: 常用的有贪心,主要是利用二进制的一些性质,即贪心最大值的尽量高位取1. 然后有前 ...
- 【CF242E】Xor Segment
题目大意:给定一个长度为 N 的序列,支持两种询问,即:区间异或,区间求和. 题解:加深了对线段树的理解. 对于线段树维护的变量一定是易于 modify 的,对于查询的答案只需用维护的东西进行组合而成 ...
随机推荐
- RAD Studio Mobile Roadmap updated,XE5 will released on next month, Andriod will be supported.
RAD Studio Mobile Roadmap updated Embarcadero updated his RAD Studio Mobile Roadmap. This concern ...
- 2017年终巨献阿里、腾讯最新Java程序员面试题,准备好进BAT了吗
Java基础 进程和线程的区别: Java的并发.多线程.线程模型: 什么是线程池,如何使用? 数据一致性如何保证:Synchronized关键字,类锁,方法锁,重入锁: Java中实现多态的机制是什 ...
- 【英语】Bingo口语笔记(84) - 惊讶的表达
- bzoj 1864 三色二叉树
Written with StackEdit. Description Input 仅有一行,不超过\(5*10^5\)个字符,表示一个二叉树序列. Output 输出文件也只有一行,包含两个数,依次 ...
- leetcode_sql_4,196
196. Delete Duplicate Emails Write a SQL query to delete all duplicate email entries in a table name ...
- 剑指Offer-第一章面试细节总结
面试细节:行为面试(20%)+技术面试(70%)+应聘者提问(10%) * 行为面试:跳槽者(不要抱怨老板,不要抱怨同事,只为追寻自己的理想而站斗) * 技术面试:1.基础知识点(编程语言,数据结构( ...
- ERP与MES
EAS-ERP企业资源计划系统 能将企业的客户管理.商品管理.采购管理.仓储管理.销售管理.生产管理.应收应付.财务管理.工资管理.费用管理和业绩考核管理以及业务预警和全方位的分析汇总融为一体,为企业 ...
- fn project 数据库配置
Databases We currently support the following databases and they are passed in via the DB_URL environ ...
- rac ASM下最简单归档开启/关闭方法
原创作品,出自 “深蓝的blog” 博客,深蓝的blog:http://blog.csdn.net/huangyanlong/article/details/47172639本次先来介绍一下在rac环 ...
- [转]javascript中基本类型和引用类型的区别分析
基本类型和引用类型 ECMAScript包含两个不同类型的值:基本类型值和引用类型值.基本类型值指的是简单的数据段:引用类型值指由多个值构成的对象.当我们把变量赋值给一个变量时,解析器首先要做的就是确 ...