bzoj 2460: 元素 线性基

本文分享了一道关于线性基的数据结构题目解析,并提供了一份完整的C++代码实现。通过线性基解决最大异或和问题,适用于算法竞赛中相似类型的题目。

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

题目大意:

http://www.lydsy.com/JudgeOnline/problem.php?id=2460

题解:

RT
线性基裸题

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
inline void read(int &x){
    x=0;char ch;bool flag = false;
    while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
    while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
inline void read(ull &x){
    x=0;char ch;bool flag = false;
    while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
    while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
const int maxn = 1024;
struct Node{
    ull num,val;
    bool friend operator < (const Node &a,const Node &b){
        return a.val > b.val;
    }
}a[maxn];
ull p[80];
inline bool check(ull x){
    for(int i = 63;i>=0;--i){
        if((x>>i)&1){
            if(p[i] == 0){
                p[i] = x;
                return true;
            }else{
                x ^= p[i];
                if(x == 0) return false;
            }
        }
    }return false;
}
int main(){
    int n;read(n);
    for(int i=1;i<=n;++i){
        read(a[i].num);
        read(a[i].val);
    }
    sort(a+1,a+n+1);
    int ans = 0;
    for(int i=1;i<=n;++i){
        if(check(a[i].num)) ans += a[i].val;
    }printf("%d\n",ans);
    getchar();getchar();
    return 0;
}

转载于:https://www.cnblogs.com/Skyminer/p/6480971.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值