SGU 275. To xor or not to xor

275. To xor or not to xor 

链接

分析:

  线性基模板题。

代码:

 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 = 61;
 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 }

猜你喜欢

转载自www.cnblogs.com/mjtcn/p/9260194.html
xor