hdu3949 XOR

http://www.elijahqi.win/archives/1681
Problem Description
XOR is a kind of bit operator, we define that as follow: for two binary base number A and B, let C=A XOR B, then for each bit of C, we can get its value by check the digit of corresponding position in A and B. And for each digit, 1 XOR 1 = 0, 1 XOR 0 = 1, 0 XOR 1 = 1, 0 XOR 0 = 0. And we simply write this operator as ^, like 3 ^ 1 = 2,4 ^ 3 = 7. XOR is an amazing operator and this is a question about XOR. We can choose several numbers and do XOR operatorion to them one by one, then we get another number. For example, if we choose 2,3 and 4, we can get 2^3^4=5. Now, you are given N numbers, and you can choose some of them(even a single number) to do XOR on them, and you can get many different numbers. Now I want you tell me which number is the K-th smallest number among them.

Input
First line of the input is a single integer T(T<=30), indicates there are T test cases.
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.

Output
For each test case,first output Case #C: in a single line,C means the number of the test case which is from 1 to T. Then for each query, you should output a single line contains the Ki-th smallest number in them, if there are less than Ki different numbers, output -1.

Sample Input
2 2 1 2 4 1 2 3 4 3 1 2 3 5 1 2 3 4 5

Sample Output
Case #1: 1 2 3 -1 Case #2: 0 1 2 3 -1

Hint
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.

Author
elfness

Source
2011 Multi-University Training Contest 11 - Host by UESTC

Recommend
xubiao | We have carefully selected several similar problems for you: 3946 3948 3947 3945 3944
题意 要求求出子集中 第k小的异或值是多少
首先求出线性基 然后根据类似二进制的原理 观察1的个数 然后可以算出我凑出的异或值是第几大的
注意 当线性基个数小于n时会存在0 所以要将询问第k小改为询问第K-1小
剩下题解来自zhx大佬
首先线性基很妙啊。可以来这里学习下:传送门
然后板子基本就是高斯消元?蒟蒻还不会高斯消元。。。待填坑。反正就是用log w级别的数,代替了原来的n个数。在什么意义上这两个东西相等呢?就是这log w个数(叫做线性基),通过线性组合可以得到所有的n个数。一般用于求解异或和最值的问题。板子见 传送门。此题是求第k小的异或和。根据线性基的性质,我们可以得到第k小的就是:把m个线性基从小到大排序,分别叫v0,v1,…vm−1,k的二进制表示为(bx….b1b0),则答案就是∑xi=0vi∗bi。注意特判0.

#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
#define N 11000
using namespace std;
inline char gc(){
    static char now[1<<16],*S,*T;
    if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}
    return *S++;
}
inline ll read(){
    ll x=0,f=1;char ch=gc();
    while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=gc();}
    while (ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=gc();}
    return x*f;
}
ll a[N],p[N],T,b[N],n,QQ,v[N];
inline bool cmp(ll aa,ll bb){return aa>bb;}
int main(){
    freopen("hdu3949.in","r",stdin);
    T=read();int tot=0;
    while(T--){
        printf("Case #%d:\n",++tot);int num=0;
        n=read();ll M=0;memset(b,0,sizeof(b));
        for (int i=1;i<=n;++i) a[i]=read(),M=max(M,a[i]);QQ=read();
        int len=log2(M-1)+1,cnt=0;
        for (int i=1;i<=n;++i){
            for (int j=60;j>=0;--j){
                if (a[i]>>j){
                    if (b[j]) a[i]^=b[j];else{
                        b[j]=a[i];cnt++;
                        for (int k=j-1;k>=0;--k) if (b[k]&&((b[j]>>k)&1)) b[j]^=b[k];
                        for (int k=j+1;k<=60;++k) if ((b[k]>>j)&1) b[k]^=b[j];break;
                    }
                }
            }
        }

        for (int i=0;i<=60;++i) if (b[i]) v[++num]=b[i];bool flag=num<n;
    //  for (int i=0;i<=num;++i) printf("%lld ",v[i]);printf("\n");
        for (int i=1;i<=QQ;++i){
            ll k=read();k-=flag;int now=0,tot1=0;num=1;
            for (int j=0;j<=60;++j) if (b[j]) ++tot1;
            if (k>=(1LL<<tot1)) {printf("-1\n");continue;}
            ll ans=0;now=0;
            /*for (;k;k>>=1){
                while(!b[now]&&now<=len) ++now;
                if (k&1) ans^=b[now];now++;
            } */
            for (;k;k>>=1){
                if (k&1) ans^=v[num];num++;
            }
            printf("%lld\n",ans);   
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值