sgu 275. To xor or not to xor 线性基

本文介绍了一种寻找整数序列中具有最大异或值子序列的算法实现。通过使用位运算和动态规划思想,该算法能在给定的时间和空间限制内找到使异或结果最大的子序列。

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

275. To xor or not to xor
time limit per test: 0.25 sec.
memory limit per test: 65536 KB
input: standard
output: standard



The sequence of non-negative integers A1, A2, ..., AN is given. You are to find some subsequence Ai1, Ai2, ..., Aik (1 <= i1 < i2 < ... < ik <= N) such, that Ai1 XOR Ai2 XOR ... XOR Aik has a maximum value.

Input
The first line of the input file contains the integer number N (1 <= N <= 100). The second line contains the sequence A1, A2, ..., AN (0 <= Ai <= 10^18). 

Output
Write to the output file a single integer number -- the maximum possible value of Ai1 XOR Ai2 XOR ... XOR Aik

Sample test(s)

Input
3 11 9 5 
Output
14 



#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

ll b[105];
const int M=(int)(log(1e18)/log(2))+1;

void solve(){
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        ll a;
        scanf("%lld",&a);
        for(int j=M;j>=0;j--){
            if((a>>j)&1){
                if(b[j]){
                    a^=b[j];
                }
                else{
                    b[j]=a;
                    for(int k=j-1;k>=0;k--){
                        if((b[j]>>k&1)&&b[k]){
                            b[j]^=b[k];
                        }
                    }
                    for(int k=j+1;k<=M;k++){
                        if(b[k]>>j&1){
                            b[k]^=b[j];
                        }
                    }
                    break;

                }
            }
        }
    }

    ll ans=0;
    for(int i=0;i<=M;i++){
        ans^=b[i];
    }
    printf("%lld",ans);
}

int main(){
    solve();
    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值