CC

自然万物都趋向从有序变得无序

0%

SGU-275 To xor or not to xor (线性基)

题目链接


Time limit 250 ms Memory limit 65536 kB

Problem Description

The sequence of non-negative integers A1, A2, …, AN is given. You are to find some subsequence $Ai_1, Ai_2, …, Ai_k (1 <= i_1 < i_2 < … < i_k <= N) such, that Ai_1 XOR Ai_2 XOR … XOR Ai_k $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 Ai1XORAi2XOR...XORAikAi_1 XOR Ai_2 XOR ... XOR Ai_k.

Sample intput

3

11 9 5

Sample Output

14


题意:

在n个数中选出任意个数,求最大异或和。

思路:

线性基模板题

推荐两篇极好的线性基入门博客

https://blog.csdn.net/qq_34374664/article/details/78498982

https://www.zybuluo.com/KirinBill/note/1054261

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*************************************************************************
> File Name: SGU-275.cpp
> Author: wood
> Mail: cbcruel@gmail.com
> Created Time: 2018年08月07日 星期二 10时58分46秒
************************************************************************/

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#include<string>

using namespace std;
typedef long long LL;
#define sqr(x) (x)*(x)
const double eps = 1e-8;
const double C = 0.57721566490153286060651209;
const double pi = acos(-1.0);
const LL mod = 1e9 + 7;
const int maxn = 1e5 + 10;
const int MAX_BASE = 62;

LL a[110],b[110];

LL solve(int n) {
for (int i = 0; i < n; ++i)
for (int j = MAX_BASE; j >= 0; --j)
if (a[i] >> j & 1) {
if (b[j]) a[i] ^= b[j];
else {
b[j] = a[i];
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 <= MAX_BASE; ++k)
if (b[k] >> j & 1)
b[k] ^= b[j];
break;
}
}
LL ans = 0;
for(int i = 0; i <= MAX_BASE; ++i)
ans ^= b[i];
return ans;
}


int main(){
int n;
scanf("%d", &n);
for(int i = 0; i < n; ++i)
scanf("%lld",a+i);
printf("%lld\n",solve(n));
return 0;
}