SGU 275. To xor or not to xor

275. To xor or not to xor 

链接

题意:

  给n个数字,问任意异或后能够组成的最大值。

分析:

  线性基模板题。

  将每个数的二进制看成一个向量,可以高斯消元得到线性基,复杂度$O(60^2n)$,每次去找当前位上为1的一个数,然后和其他的所有数异或。

  但是可以$O(60n)$的求线性基。依次扫每个向量,然后如果这位没有1,就将它放到这一位上,否则异或这一位上的数。这样会得到一个上三角的矩阵,和高斯消元是一样的,也可以消成对角矩阵。

  attack的模拟赛虽然四道出锅了三道,唯一没出锅的T3线性基还不会,于是考试剩余的时间学了线性基。。练一下模板!

代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 
 5 const int N = 100 + 10;
 6 const int m = 62;
 7 LL a[N],b[N];
 8 int n;
 9 
10 void build() {
11     for (int i=1; i<=n; ++i) 
12         for (int j=m; j>=0; --j) 
13             if ((1LL<<j)&a[i]) { 
14                 if (b[j]) a[i] ^= b[j];
15                 else {
16                     b[j] = a[i];
17                     for (int k=j-1; k>=0; --k) if (b[k]&&((1LL<<k)&b[j])) b[j] ^= b[k];
18                     for (int k=j+1; k<=m; ++k) if ((1LL<<j)&b[k]) b[k] ^= b[j];
19                     break;
20                 }
21             }
22         
23 }
24 void solve() {
25     LL ans = 0;
26     for (int i=0; i<=m; ++i) 
27         if (b[i]) ans ^= b[i];
28     cout << ans;
29 }
30 int main() {
31     scanf("%d",&n);
32     for (int i=1; i<=n; ++i) scanf("%I64d",&a[i]);
33     build();
34     solve();    
35     return 0;
36 }
原文地址:https://www.cnblogs.com/mjtcn/p/9260194.html