题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3949

题意:给出一个长度为n的数列A。选出A的所有子集(除空集外)进行抑或得到2^n-1个数字,去重排序得到B。给出一个数K,询问在B中第K个数字是多少?

思路:首先看做n个二进制的数字进行高斯消元,得到k个线性无关的数字。然后由这k个线性无关的数字得到第K个数字。这里不能选择空集,因此对于B中是否存在0要进行特判。

#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <map>
#include <ctype.h>
#include <time.h>

#define abs(x) ((x)>=0?(x):-(x))
#define i64 long long
#define u32 unsigned int
#define u64 unsigned long long
#define clr(x,y) memset(x,y,sizeof(x))
#define CLR(x) x.clear()
#define ph(x) push(x)
#define pb(x) push_back(x)
#define Len(x) x.length()
#define SZ(x) x.size()
#define PI acos(-1.0)
#define sqr(x) ((x)*(x))
#define MP(x,y) make_pair(x,y)
#define EPS 1e-10
    
    
#define FOR0(i,x) for(i=0;i<x;i++)
#define FOR1(i,x) for(i=1;i<=x;i++)
#define FOR(i,a,b) for(i=a;i<=b;i++)
#define FORL0(i,a) for(i=a;i>=0;i--)
#define FORL1(i,a) for(i=a;i>=1;i--)
#define FORL(i,a,b)for(i=a;i>=b;i--)
    
    
#define rush() int CC;for(scanf("%d",&CC);CC--;)
#define Rush(n)  while(scanf("%d",&n)!=-1)
using namespace std;
    
void RD(int &x){scanf("%d",&x);}
void RD(i64 &x){scanf("%I64d",&x);}
void RD(u64 &x){scanf("%I64u",&x);}
void RD(u32 &x){scanf("%u",&x);}
void RD(double &x){scanf("%lf",&x);}
void RD(int &x,int &y){scanf("%d%d",&x,&y);}
void RD(i64 &x,i64 &y){scanf("%lld%lld",&x,&y);}
void RD(u32 &x,u32 &y){scanf("%u%u",&x,&y);}
void RD(double &x,double &y){scanf("%lf%lf",&x,&y);}
void RD(double &x,double &y,double &z){scanf("%lf%lf%lf",&x,&y,&z);}
void RD(int &x,int &y,int &z){scanf("%d%d%d",&x,&y,&z);}
void RD(i64 &x,i64 &y,i64 &z){scanf("%lld%lld%lld",&x,&y,&z);}
void RD(u32 &x,u32 &y,u32 &z){scanf("%u%u%u",&x,&y,&z);}
void RD(char &x){x=getchar();}
void RD(char *s){scanf("%s",s);}
void RD(string &s){cin>>s;}
    
    
void PR(int x) {printf("%d\n",x);}
void PR(int x,int y) {printf("%d %d\n",x,y);}
void PR(i64 x) {printf("%I64d\n",x);}
void PR(i64 x,i64 y) {printf("%lld %lld\n",x,y);}
void PR(u32 x) {printf("%u\n",x);}
void PR(u64 x) {printf("%llu\n",x);}
void PR(double x) {printf("%.10lf\n",x);}
void PR(double x,double y) {printf("%.5lf %.5lf\n",x,y);}
void PR(char x) {printf("%c\n",x);}
void PR(char *x) {printf("%s\n",x);}
void PR(string x) {cout<<x<<endl;}

void upMin(int &x,int y) {if(x>y) x=y;}
void upMin(i64 &x,i64 y) {if(x>y) x=y;}
void upMin(double &x,double y) {if(x>y) x=y;}
void upMax(int &x,int y) {if(x<y) x=y;}
void upMax(i64 &x,i64 y) {if(x<y) x=y;}
void upMax(double &x,double y) {if(x<y) x=y;}
    
const int mod=10086;
const i64 inf=((i64)1)<<60;
const double dinf=1e10;
const int INF=100000000;
const int N=100005;

int n,m,flag;
i64 a[N];

int Gauss()
{
    flag=1;
    int i,j=0,k;
    i64 p=1;
    for(i=63;i>=0;i--)
    {
        for(k=j;k<n;k++) if(a[k]&(p<<i)) break;
        if(k>=n) continue;
        swap(a[j],a[k]);
        for(k=0;k<n;k++) if(j!=k&&(a[k]&(p<<i))) 
        {
            a[k]^=a[j];
            if(a[k]==0) flag=0;
        }
        j++;
    }
    return j;
}

int main()
{
    int num=0;
    rush()
    {
        RD(n);
        int i;
        FOR0(i,n) RD(a[i]);
        int k=Gauss();
        RD(m);
        printf("Case #%d:\n",++num);
        i64 K,p=1,ans;
        while(m--)
        {
            RD(K);
            if(K>(p<<k)-flag) puts("-1");
            else if(!flag&&K==1) puts("0");
            else
            {
                if(!flag) K--;
                ans=0;
                for(i=0;i<k;i++) if(K>=(p<<(k-i-1)))
                {
                    K-=(p<<(k-i-1));
                    ans^=a[i];
                    if(K==0) break;
                }
                PR(ans);
            }
        }
    }
}

HDU 3949 XOR(高斯消元)的更多相关文章

  1. HDU 3949 XOR [高斯消元XOR 线性基]

    3949冰上走 题意: 给你 N个数,从中取出若干个进行异或运算 , 求最后所有可以得到的异或结果中的第k小值 N个数高斯消元求出线性基后,设秩为$r$,那么总共可以组成$2^r$中数字(本题不能不选 ...

  2. HDU 3949 XOR 高斯消元

    题目大意:给定一个数组,求这些数组通过异或能得到的数中的第k小是多少 首先高斯消元求出线性基,然后将k依照二进制拆分就可以 注意当高斯消元结束后若末尾有0则第1小是0 特判一下然后k-- 然后HDU输 ...

  3. ACM学习历程—HDU 3949 XOR(xor高斯消元)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3949 题目大意是给n个数,然后随便取几个数求xor和,求第k小的.(重复不计算) 首先想把所有xor的 ...

  4. ACM学习历程—HDU 3915 Game(Nim博弈 && xor高斯消元)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3915 题目大意是给了n个堆,然后去掉一些堆,使得先手变成必败局势. 首先这是个Nim博弈,必败局势是所 ...

  5. bzoj 2115: [Wc2011] Xor xor高斯消元

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

  6. ACM学习历程—UESTC 1219 Ba Gua Zhen(dfs && 独立回路 && xor高斯消元)

    题目链接:http://acm.uestc.edu.cn/#/problem/show/1219 题目大意是给了一张图,然后要求一个点通过路径回到这个点,使得xor和最大. 这是CCPC南阳站的一道题 ...

  7. ACM学习历程—BZOJ 2115 Xor(dfs && 独立回路 && xor高斯消元)

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2115 题目大意是求一条从1到n的路径,使得路径xor和最大. 可以发现想枚举1到n的所有路 ...

  8. ACM学习历程—SGU 275 To xor or not to xor(xor高斯消元)

    题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=275 这是一道xor高斯消元. 题目大意是给了n个数,然后任取几个数,让他们xor和 ...

  9. hdu3949 XOR xor高斯消元

    XOR Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  10. SGU 275 To xor or not to xor 高斯消元求N个数中选择任意数XORmax

    275. To xor or not to xor   The sequence of non-negative integers A1, A2, ..., AN is given. You are ...

随机推荐

  1. 【js】随机数

    <script>   function GetRandomNum(Min,Max){   var Range = Max - Min;   var Rand = Math.random() ...

  2. ios开发之NavBar和TarBar使用技巧

    1  改变NavBar颜色:选中Navigation Bar 的Tint属性.选中颜色. 2  隐藏“back”按钮: self.navigationItem.hidesBackButton = YE ...

  3. UpdateData(false) and UpdateData(true)

    数据更新函数: UpdateData(false); 控件的关联变量的值传给控件并改变控件状态(程序--->EXE) UpdateData(true); 控件的状态传给其关联的变量(EXE--- ...

  4. SVN服务器搭建和使用

    SVN服务器搭建和使用 Subversion是优秀的版本控制工具,其具体的的优点和详细介绍,这里就不再多说. 首先来下载和搭建SVN服务器. 现在Subversion已经迁移到apache网站上了,下 ...

  5. Useful related java API for Android

    Language_suport and Other Language-Oriented API: strings,exceptions, threads, #java.lang.* offers th ...

  6. Unity物理优化的一个小问题

    为了性能优化,Unity会计算场景中所有的体积,包括静态的碰撞器并把 这些信息存在“Cash”中,对于静止物体而言,有了这些信息,就不需要 再每帧重新进行计算了.若移动,拉伸后或旋转了静态物体时,就是 ...

  7. 剑指offer--面试题14

    #include "stdafx.h" #include <iostream> using namespace std; //调整数组顺序使奇数位于偶数前 void O ...

  8. soft particles

    http://developer.download.nvidia.com/whitepapers/2007/SDK10/SoftParticles_hi.pdf nvdia ------------- ...

  9. jquery css 简单笔记

    内容 要点:清空表单中所有数据,除去 button,submit,reset,hidden 的数据 $(':input','#myform') .not(':button, :submit, :res ...

  10. ERROR: JDWP Unable to get JNI 1.2 environment, jvm->GetEnv() return code = -2 JDWP exit error AGENT_ERROR_NO_JNI_ENV(183):

    eclipse中进行java debug调试时出现上述问题. solution:请在代码最后加入以下语句:System.exit(0)即可.