SGU 275 To xor or not to xor

题意:

  从n个数中选若干个数,使它们的异或和最大。n<=100



Solution

经典的异或高斯消元。

//O(60*n)
#include <iostream>
using namespace std;
int n;
long long a[109];

int main()
{
    ios::sync_with_stdio();
    cin >> n;
    long long ans = 0;
    for (int i = 1; i <= n; ++i) cin >> a[i];
    for (int i = 62; i >= 0; --i) {
        for (int j = 1; j <= n; ++j) {
            if (a[j] & 1LL << i ) {
                long long t = a[j];
                if (! (ans & 1LL << i ) ) ans ^= t;
                for (int k = j; k <= n; ++k) {
                    if (a[k] & 1LL << i )
                        a[k] ^= t;
                }
            }
        }
    }
    cout << ans << endl;
    return 0;
}

O(63*n)

时间: 2024-12-28 21:54:28

SGU 275 To xor or not to xor的相关文章

SGU 275 To xor or not to xor 高斯消元求N个数中选择任意数XORmax

275. To xor or not to xor 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.

SGU 275 To xor or not to xor (高斯消元)

题目地址:SGU 275 首先,贪心的思想,每一二进制位上要尽量是1,而能不能是1用高斯消元来解决.当该位有一个可以使之为1的变元时,就说明这位可以为1,而且令该变元控制该位,然后向低位消元. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.h>

sgu To xor or not to xor

题意:从n个数中,选择一些数,使得异或最大. 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #define ll __int64 5 using namespace std; 6 7 ll c[110][110]; 8 int n; 9 ll cc; 10 11 void gauss() 12 { 13 int i,j; 14 ll ans=0; 15 for(int r=0; r&l

SGU 275 To xor or not to xor (高斯消元)

题目链接 题意: 分析: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <cmath> 6 #include <algorithm> 7 #define LL __int64 8 const int maxn = 100+10; 9 using namespace std; 10 int a[

SGU 275 To xor or not to xor【最大xor和 高斯消元】

题目大意:给你n个数(n<=100)要你找出若干个数使他们的异或和最大 思路:高斯-若当消元消完以后削成若干个独立的行向量,将它们异或起来就好 #include<cstdio> #include<string.h> #include<iostream> #include<algorithm> #define maxn 3000 #define LL __int64 using namespace std; LL bin[maxn],a[maxn]; i

BZOJ 2115: [Wc2011] Xor [高斯消元XOR 线性基 图]

啦啦啦 题意: N 个点M条边的边带权的无向图,求1到n一条XOR和最大的路径 感觉把学的东西都用上了.... 1到n的所有路径可以由一条1到n的简单路径异或上任意个简单环得到 证明: 如果环与路径有交,异或后那块交就没了,相当于那块走了环上的路径: 如果环与路径没交,就是走到环上走一圈在回来,一去一回其他的地方又没了. 求一棵生成树,然后每一条非树边构成一个环,一共$m-n+1$个环 然后答案就是任取一些环的异或和与1到n路径异或和异或的最大值啦 实现上注意: 1.求生成树和简单环的异或和一遍

[hdu3949]XOR(线性基求xor第k小)

题目大意:求xor所有值的第k小,线性基模板题. #include<cstdio> #include<cstring> #include<algorithm> #include<cstdlib> #include<iostream> #include<cmath> using namespace std; typedef long long ll; const int MAX_BASE=63; ll base[64],a[10006]

4269: 再见Xor

4269: 再见Xor 链接 分析: 和SGU 275唯一不同的就是需要求出次小值,那么异或出最大值的所有元素中,找到最小的,去除即可. 代码: 1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 5 const int N = 100000 + 10; 6 const int m = 30; 7 LL a[N],b[N]; 8 int n; 9 10 void build() { 11 for

bzoj 2337: [HNOI2011]XOR和路径

Description Input Output Sample Input Sample Output HINT Source Day2 终于把这个史前遗留的坑给填了... 首先异或的话由位无关性,可以按位处理... 那么对于每一位,设f[i]表示从i出发第一次到达n且xor和为1的概率,out[i]为i的出边,那么转移就比较容易了... if(w(i,j)&xxx) f[i]+=(1-f[j)/out[i];// 这条边该位为1,需要xor上0,xor和才为1 else f[i]+=f[j]/