0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-23 18:48:48 +00:00

P3812 【模板】线性基

https://www.luogu.com.cn/record/103041260
This commit is contained in:
Baoshuo Ren 2023-02-26 08:39:10 +08:00
parent 51e252392e
commit b1fdc7d76e
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A
3 changed files with 46 additions and 23 deletions

View File

@ -1,50 +1,67 @@
#include <iostream>
#include <algorithm>
#include <iterator>
#include <limits>
#include <type_traits>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 55;
const int N = 1e5 + 5;
int n;
long long x, a[N], ans;
template <typename T>
struct LinearBasis
: std::enable_if<std::is_unsigned<T>::value, T> {
T a[std::numeric_limits<T>::digits];
void insert(long long x) {
for (int i = 50; ~i; i--) {
if (!(x & (1ll << i))) continue;
LinearBasis() {
std::fill(std::begin(a), std::end(a), 0);
}
if (a[i]) {
x ^= a[i];
} else {
for (int k = 0; k < i; k++) {
if (x & (1ll << k)) x ^= a[k];
void insert(unsigned long long x) {
for (int i = std::numeric_limits<T>::digits - 1; ~i; i--) {
if ((x >> i) & 1) {
if (a[i]) {
x ^= a[i];
} else {
a[i] = x;
return;
}
}
for (int k = i + 1; k <= 50; k++) {
if (a[k] & (1ll << i)) a[k] ^= x;
}
a[i] = x;
return;
}
}
}
T query() {
T res(0);
for (int i = std::numeric_limits<T>::digits - 1; ~i; i--) {
if ((res ^ a[i]) > res) res ^= a[i];
}
return res;
}
};
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
LinearBasis<unsigned long long> lb;
cin >> n;
for (int i = 1; i <= n; i++) {
unsigned long long x;
cin >> x;
insert(x);
lb.insert(x);
}
for (int i = 0; i <= 50; i++) ans ^= a[i];
cout << ans << endl;
cout << lb.query() << endl;
return 0;
}

BIN
Luogu/P3812/data/P3812_2.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Luogu/P3812/data/P3812_2.out (Stored with Git LFS) Normal file

Binary file not shown.