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<61; r++)
16     {
17         c[r][n]=1;
18         for(i=0; i<r; i++)
19         {
20             for(j=0; j<n; j++)
21             {
22                 if(c[i][j])
23                     break;
24             }
25             if(j<n&&c[r][j])
26             {
27                 for(; j<=n; j++)
28                 {
29                     c[r][j]^=c[i][j];
30                 }
31             }
32         }
33         for(i=0; i<n; i++)
34         {
35             if(c[r][i]) break;
36         }
37         if(i<n||(i==n&&!c[r][n]))
38         {
39             ans|=1ll<<(60-r);
40         }
41     }
42     printf("%I64d\n",ans);
43 }
44
45 int main()
46 {
47     while(scanf("%d",&n)!=EOF)
48     {
49         for(int i=0; i<n; i++)
50         {
51             scanf("%I64d",&cc);
52             for(int j=60; j>=0; j--)
53             {
54                 c[60-j][i]=(cc>>j)&1;
55             }
56         }
57         gauss();
58     }
59     return 0;
60 }

sgu To xor or not to xor

时间: 2024-12-25 16:24:03

sgu 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 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

题意: 从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

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]

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]/

xor异或逻辑操作(辅助完成图形的叠加)

异或操作的作用: 异或 两个不相同,返回true, 两个相同返回false 0 xor 0  = 0 0 xor 1  = 1 1 xor 0  = 1 1 xor 1  = 0 特殊情况, 全0的2*2矩阵,  一个其它矩阵和它xor的话是其本身: 依据 0 xor 0 =  0, 1 xor 0 = 1.   这个其它矩阵值不变. 0   0 0   0 特殊情况, 全1的2*2矩阵,  一个其它矩阵和它xor的话是其相反的值: 依据 0 xor 1 = 1,  1 xor 1 = 0,