题目链接: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. 洛谷P3943 星空

    洛谷P3943 星空 题目背景 命运偷走如果只留下结果, 时间偷走初衷只留下了苦衷. 你来过,然后你走后,只留下星空. 题目描述 逃不掉的那一天还是来了,小 F 看着夜空发呆. 天上空荡荡的,没有一颗 ...

  2. vue介绍和简单使用

    Vue是什么? Vue (读音 /vjuː/,类似于 view) 是一套用于构建用户界面的渐进式框架.与其它大型框架不同的是,Vue 被设计为可以自底向上逐层应用.Vue 的核心库只关注视图层,不仅易 ...

  3. Symfony 使用KnpTimeBundle

    使用time_diff时出现:diff.ago.hour; 解决:1:引入"knplabs/knp-time-bundle": "^1.7",https://g ...

  4. 批处理设置IP地址

    echo offecho 修改[本地连接]IP......netsh interface IP set address "本地连接" static 138.8.8.111 255. ...

  5. 查看当前.net版本 cmd

    reg query "HKLM\Software\Microsoft\NET Framework Setup\NDP" /s /v version | findstr /i ver ...

  6. Crontab使用详解

    第1列分钟1-59第2列小时1-23(0表示子夜)第3列日1-31第4列月1-12第5列星期0-6(0表示星期天)第6列要运行的命令 下面是crontab的格式:分 时 日 月 星期 要运行的命令 这 ...

  7. rails 常用方法

    bundle install --without production 不安装production中的gem ./configure && make && sudo m ...

  8. 在linux系统下Git源码系统的文件下载

    Git是一个开源的分布式版本控制系统,在linux系统中下载git中的文件使用repo的很多. 网上有很多repo下载的地址失效,目前可用的链接在这里记录一下. 没有安装git的安装一下: sudo ...

  9. [原创]java WEB学习笔记16:JSP指令(page,include),JSP标签(forwar,include,param)

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...

  10. 对unidbgrid的单元格操作

    一.使某行某列单元格disabled: 1. UniStringGrid -> Options -> goEditing = true 2. UniStringGrid -> Ext ...