题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=275

这是一道xor高斯消元。

题目大意是给了n个数,然后任取几个数,让他们xor和最大。

首先根据题目意思可以列出下列方程组:

//a11x1+a21x2……=d[1]

//a12x1+a22x2……=d[2]

//...

(每个数二进制按列来写,xi为0或1,表示取或不取这个数。)

结果的二进制即为d数组。

由于需要结果最大,而结果最多是d全为1,那么就假设所有d均为1,然后进行高斯消元,来判断该行的d是否能取到。

步骤如下:

1、建立增广矩阵。

2、从最后一行往前扫,如果该行存在1,那么d[i]自然能取到1,这样需要把该列其它的1消掉,由于是高斯消元,消1的时候需要整行消;如果该行不存在1,而且d[i] == 0,自然该行的方程仍然有解。

3、消元的过程中保存答案。

复杂度O(63*63n)

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <string>
#define LL long long using namespace std; const int len = ;
int n, a[][];
bool vis[]; void xorGauss()
{
LL ans = ;
for (int i = len-; i >= ; i--)
{
int j;
for (j = ; j < n; j++)
{
if (a[i][j] && !vis[j])
{
vis[j] = true;
ans += (LL)<<i;
break;
}
}
if(j == n)
{
if(a[i][n] == )
ans += (LL)<<i;
}
else
{
for (int k = i-; k >= ; k--)
{
if (a[k][j])
{
for (int v = ; v <= n; v++)
a[k][v] ^= a[i][v];
}
}
}
}
printf("%I64d\n", ans);
} void input()
{
memset(a, , sizeof(a));
memset(vis, false, sizeof(vis));
//next is input
LL v;
int k;
for (int i = ; i < n; i++)
{
scanf("%I64d", &v);
for (int j = ; v > ; j++)
{
k = v&;
a[j][i] = k;
v >>= ;
}
}
//pre is input
for (int i = ; i < len; i++)
a[i][n] = ;
} int main()
{
// freopen("test.in", "r", stdin);
while (scanf("%d", &n) != EOF)
{
input();
xorGauss();
}
return ;
}

其实到这里这个问题并没有完美解决。

起始从前面的方程组可以看出来,一组矩阵,可以等同于另一组等价的矩阵。

于是我们只需要找出这个矩阵里面的最大线性无关组。(数之间不能互相表示)

然后通过线性无关组就能表示最大值了。

其实就是把矩阵化成最简矩阵。

然后这时把一个数看成整体,就能用位运算优化了。

效率O(63n)

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <string>
#define LL long long using namespace std; //xor高斯消元求线性基
//时间复杂度O(63n)
const int maxN = ;
int n;
LL a[maxN]; int xorGauss(int n)
{
int row = ;
for (int i = ; i >= ; i--)
{
int j;
for (j = row; j < n; j++)
if(a[j]&((LL)<<i))
break;
if (j != n)
{
swap(a[row], a[j]);
for (j = ; j < n; j++)
{
if(j == row) continue;
if(a[j]&((LL)<<i))
a[j] ^= a[row];
}
row++;
}
}
return row;
} void work()
{
for (int i = ; i < n; i++)
scanf("%I64d", &a[i]);
int row;
row = xorGauss(n);
LL ans = ;
for (int i = ; i < row; ++i)
ans = max(ans, ans^a[i]);
printf("%I64d\n", ans);
} int main()
{
//freopen("test.in", "r", stdin);
while (scanf("%d", &n) != EOF)
{
work();
}
return ;
}

ACM学习历程—SGU 275 To xor or not to xor(xor高斯消元)的更多相关文章

  1. BZOJ2337 [HNOI2011]XOR和路径 【概率dp + 高斯消元】

    题目 题解 突然get到这样路径期望的题目八成是高斯消元 因为路径上的dp往往具有后效性,这就形成了一个方程组 对于本题来说,直接对权值dp很难找到突破口 但是由于异或是位独立的,我们考虑求出每一位的 ...

  2. 洛谷P3211 [HNOI2011]XOR和路径(期望dp+高斯消元)

    传送门 高斯消元还是一如既往的难打……板子都背不来……Kelin大佬太强啦 不知道大佬们是怎么发现可以按位考虑贡献,求出每一位是$1$的概率 然后设$f[u]$表示$u->n$的路径上这一位为$ ...

  3. ACM学习历程—HDU 3915 Game(Nim博弈 && xor高斯消元)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3915 题目大意是给了n个堆,然后去掉一些堆,使得先手变成必败局势. 首先这是个Nim博弈,必败局势是所 ...

  4. ACM学习历程—UESTC 1219 Ba Gua Zhen(dfs && 独立回路 && xor高斯消元)

    题目链接:http://acm.uestc.edu.cn/#/problem/show/1219 题目大意是给了一张图,然后要求一个点通过路径回到这个点,使得xor和最大. 这是CCPC南阳站的一道题 ...

  5. ACM学习历程—HDU 3949 XOR(xor高斯消元)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3949 题目大意是给n个数,然后随便取几个数求xor和,求第k小的.(重复不计算) 首先想把所有xor的 ...

  6. ACM学习历程—BZOJ 2115 Xor(dfs && 独立回路 && xor高斯消元)

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2115 题目大意是求一条从1到n的路径,使得路径xor和最大. 可以发现想枚举1到n的所有路 ...

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

  8. SGU 200 Cracking RSA (高斯消元)

    转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove 题意:给出m个整理,因子全部为前t个素数.问有多少 ...

  9. HDU 3949:XOR(高斯消元+线性基)

    题目链接 题意 给出n个数,问这些数的某些数xor后第k小的是谁. 思路 高斯消元求线性基. 学习地址 把每个数都拆成二进制,然后进行高斯消元,如果这个数字这一位(列)有1,那么让其他数都去异或它,消 ...

随机推荐

  1. Swift开发教程--怎样设置状态栏的文字颜色

    第一步:在Info.plist中设置UIViewControllerBasedStatusBarAppearance 为NO 第二步:在viewDidLoad中加一句 UIApplication.sh ...

  2. SET IDENTITY_INSERT ON/OFF 权限

    今天突然遇到了,找不到对象“XXXX”,因为它不存在或者没有您所需的权限,于是检查程序,突然发现程序中有一段代码是: SET IDENTITY_INSERT eticket ON //执行业务 ... ...

  3. Count(二维树状数组)

    [bzoj1452][JSOI2009]Count Description Input Output Sample Input Sample Output 12   HINT 题解:对于每一个颜色建一 ...

  4. 基于PLSQL的数据库备份方法及如何解决导出clob和blob类型数据报错的问题

    基于PL/SQL的数据库备份方法 PL/SQL Developer是Oracle 数据库中用于导入或导出数据库的主要工具,本文主要介绍了利用PL/SQL Developer导入和导出数据库的过程,并对 ...

  5. 九度OJ 1326:Waiting in Line(排队) (模拟)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:220 解决:64 题目描述: Suppose a bank has N windows open for service. There is ...

  6. [php][随机数]曲线式的随机

    数学函数原型: y = max / (x ^ 2) 函数图像(来自google): y = 100 / x ^ (-2) 其中y为随机结果,max为最大值且max>1,x为随机数, 两个参数: ...

  7. OC中第三方库MJExtension的使用

    MJExtension是一套常用的"字典和模型之间互相转换"的框架,在项目中也使用过,现在记录一下.随着Swift的普及,在Swift中也有一个类似功能的框架HandyJSON 也 ...

  8. linux 字符驱动

    1 结构体说明:     struct cdev {         struct kobject kobj;          // 每一个 cdev 都是一个 kobject         st ...

  9. iOS8 with Swift

    Ref:iOS8 Day-by-Day Ref:iOS8-day-by-day source Ref:Let's Swift Ref:Swift 代码库 Ref:iOS Apprentice Thir ...

  10. 3.09课·········for穷举和迭代

    for循环拥有两类:穷举和迭代穷举:把所有可能的情况都走一遍,使用if条件筛选出来满足条件的情况. 1.单位给发了一张150元购物卡,拿着到超市买三类洗化用品.洗发水15元,香皂2元,牙刷5元.求刚好 ...