HDU 3949 XOR 异或

本文介绍了解决HDU3949问题的方法,该问题是关于寻找一系列整数中第k小的异或值。通过将输入数据转换为异或基形式,并利用高斯消元法进行处理,可以有效地解决这个问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:给你n个数,你可以找任意个数异或,求第k小的异或值。
思路:异或好神奇。就是把n个数变成它们异或基的形式。然后就可以找出第k大的异或值。(代码中关于异或基的一些想法是我自己脑补出来。)

http://acm.hdu.edu.cn/showproblem.php?pid=3949

/*********************************************
    Problem : HDU 3949
    Author  : NMfloat
    InkTime (c) NM . All Rights Reserved .
********************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>

#define rep(i,a,b)  for(int i = (a) ; i <= (b) ; i ++) //遍历
#define rrep(i,a,b) for(int i = (b) ; i >= (a) ; i --) //反向遍历
#define repS(it,p) for(auto it = p.begin() ; it != p.end() ; it ++) //遍历一个STL容器
#define repE(p,u) for(Edge * p = G[u].first ; p ; p = p -> next) //遍历u所连接的点
#define cls(a,x)   memset(a,x,sizeof(a))
#define eps 1e-8

using namespace std;

const int MOD = 1e9+7;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e5+5;
const int MAXE = 2e5+5;

typedef long long LL;
typedef unsigned long long ULL;

int T,n,m;

int fx[] = {0,1,-1,0,0};
int fy[] = {0,0,0,-1,1};

LL a[10005];

int xorGauss() {
    // 这个异或的意义
    // 在本质上,变换后的a数组和原来的数组是等价的
    // 因为a[1] a[2] ... a[row] 是之前a数组中的某些数异或而来的
    // 所以使用 选用a[1] a[2] ... a[row] 中的某些数 异或而来的数 肯定是可以用之前a数组异或而来的
    // 假设之前的数组位b[1] b[2] ... b[n] 得到的数组位a[1] a[2] ... a[row]
    // 因为 假设 a[1] = b[1] ^ b[2]; a[2] = b[1] ^ b[3]; a[1] ^ a[2] = b[2] ^ b[3];

    int row = 1; //代表xor方程解出来有多少位
    rrep(i,0,62) { //从高位向低位处理
        int pos = 0; //代表处理的是第几个
        rep(j,row,n) {
            if(a[j] & (1LL<<i)) {pos = j ; break;}
        }
        if(pos) { //第i位存在
            swap(a[pos],a[row]); //pos 和 row相同时也没有关系
            rep(j,1,n) {
                if(j == row) continue;
                if(a[j] & (1LL<<i)) a[j] ^= a[row];
            }
            row ++;
        }
    }
    return row - 1; //因为初始是从1开始的,所有要减去一个1
    // 异或的所有结果为 2 ^ row - 1个
}

LL kth_min(LL k,int row) {
    if(n > row) {
        // 为什么n > row 的时候就产生0了
        // a[1] a[2] ... a[row] 每个代表着一个基
        // a[row+1] 必然可以a[1] a[2] ... a[row] 中的某些元素组合而来
        // 所以n > row 的时候就产生0了
        if(k == 1) return 0;
        else k --;
    }
    if(k >= (1LL<<row)) return -1;
    LL ret = 0;
    rrep(i,0,62) {
        if(k & (1LL<<i)) {
            ret ^= a[row - i];
        }
    }
    return ret;
}

void input() {
    scanf("%d",&n);
    rep(i,1,n) scanf("%I64d",&a[i]);
}

void solve() {
    int q;
    int row = xorGauss();
    scanf("%d",&q);
    LL num;
    rep(i,1,q) {
        scanf("%I64d",&num);
        printf("%I64d\n",kth_min(num,row));
    }
}

int main(void) {
    //freopen("a.in","r",stdin);
    int casenum = 0;
    scanf("%d",&T); while(T--) {
        printf("Case #%d:\n",++casenum);
        input();
        solve();
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值