版权声明:本文为博主原创文章。未经博主同意不得转载。 https://blog.csdn.net/u011686226/article/details/32337735
题目来源:Light OJ 1272 Maximum Subset Sum
题意:选出一些数 他们的抑或之后的值最大
思路:每一个数为一个方程 高斯消元 从最高位求出上三角 消元前k个a[i]异或和都能有消元后的异或和组成
消元前
k
个
a[i]
a[i]异或和都能有消元后的
异或和都能有消元后的
p
个
a[i]
a[i]的异或
的异或
保证每一列仅仅有一个1 消元后全部A[i]抑或起来就是答案#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 110;
typedef long long LL;
typedef LL Matrix[maxn];
LL x[maxn];
void gauss(Matrix A, int n)
{
int i = 0, j = 63, k, u, r;
while(i < n && j >= 0)
{
int r = i;
for(k = i; k < n; k++)
if((A[k]>>j)&1)
{
r = k;
break;
}
if((A[r]>>j)&1)
{
if(r != i)
swap(A[i], A[r]);
for(u = 0; u < n; u++)
{
if(u != i && (A[u]>>j)&(A[i]>>j))
A[u] ^= A[i];
}
i++;
}
j--;
}
}
Matrix A;
int main()
{
int cas = 1;
int T;
scanf("%d", &T);
while(T--)
{
int n;
scanf("%d", &n);
for(int i = 0; i < n; i++)
scanf("%lld", &A[i]);
gauss(A, n);
LL ans = 0;
for(int i = 0; i < n; i++)
ans ^= A[i];
printf("Case %d: %lld
", cas++, ans);
}
return 0;
}