题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3915

题目大意是给了n个堆,然后去掉一些堆,使得先手变成必败局势。

首先这是个Nim博弈,必败局势是所有xor和为0.

那么自然变成了n个数里面取出一些数,使得xor和为0,求取法数。

首先由xor高斯消元得到一组向量基,但是这些向量基是无法表示0的。

所以要表示0,必须有若干0来表示,所以n-row就是消元结束后0的个数,那么2^(n-row)就是能组成0的种数。

对n==row特判一下。

代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <cstring>
  6. #include <algorithm>
  7. #include <set>
  8. #include <map>
  9. #include <queue>
  10. #include <string>
  11. #define LL long long
  12.  
  13. using namespace std;
  14.  
  15. const int maxN = ;
  16. const int MOD = ;
  17. int n, s[maxN];
  18.  
  19. void input()
  20. {
  21. scanf("%d", &n);
  22. for (int i = ; i < n; ++i)
  23. scanf("%d", &s[i]);
  24. }
  25.  
  26. //xor高斯消元求线性基
  27. //时间复杂度O(30n)
  28. int xorGauss(int n)
  29. {
  30. int row = ;
  31. for (int i = ; i >= ; i--)
  32. {
  33. int j;
  34. for (j = row; j < n; j++)
  35. if(s[j]&(<<i))
  36. break;
  37. if (j != n)
  38. {
  39. swap(s[row], s[j]);
  40. for (j = ; j < n; j++)
  41. {
  42. if(j == row) continue;
  43. if(s[j]&(<<i))
  44. s[j] ^= s[row];
  45. }
  46. row++;
  47. }
  48. }
  49. return row;
  50. }
  51.  
  52. void work()
  53. {
  54. int row, ans, k;
  55. row = xorGauss(n);
  56. ans = n-row;
  57. if (ans != -)
  58. {
  59. k = ;
  60. while (ans)
  61. {
  62. k <<= ;
  63. k %= MOD;
  64. ans--;
  65. }
  66. ans = k;
  67. }
  68. else
  69. ans = -;
  70. printf("%d\n", ans);
  71. }
  72.  
  73. int main()
  74. {
  75. //freopen("test.in", "r", stdin);
  76. int T;
  77. scanf("%d", &T);
  78. for (int times = ; times < T; ++times)
  79. {
  80. input();
  81. work();
  82. }
  83. return ;
  84. }

ACM学习历程—HDU 3915 Game(Nim博弈 && xor高斯消元)的更多相关文章

  1. ACM学习历程—HDU 5536 Chip Factory(xor && 字典树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题目大意是给了一个序列,求(si+sj)^sk的最大值. 首先n有1000,暴力理论上是不行的. ...

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

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

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

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

  4. ACM学习历程—SGU 275 To xor or not to xor(xor高斯消元)

    题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=275 这是一道xor高斯消元. 题目大意是给了n个数,然后任取几个数,让他们xor和 ...

  5. HDU 5833 Zhu and 772002 (高斯消元)

    Zhu and 772002 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5833 Description Zhu and 772002 are b ...

  6. 2016ACM/ICPC亚洲区沈阳站H - Guessing the Dice Roll HDU - 5955 ac自动机+概率dp+高斯消元

    http://acm.hdu.edu.cn/showproblem.php?pid=5955 题意:给你长度为l的n组数,每个数1-6,每次扔色子,问你每个串第一次被匹配的概率是多少 题解:先建成ac ...

  7. HDU 3949 XOR 高斯消元

    题目大意:给定一个数组,求这些数组通过异或能得到的数中的第k小是多少 首先高斯消元求出线性基,然后将k依照二进制拆分就可以 注意当高斯消元结束后若末尾有0则第1小是0 特判一下然后k-- 然后HDU输 ...

  8. ACM学习历程—HDU 5512 Pagodas(数学)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5512 学习菊苣的博客,只粘链接,不粘题目描述了. 题目大意就是给了初始的集合{a, b},然后取集合里 ...

  9. ACM学习历程—HDU 5534 Partial Tree(动态规划)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5534 题目大意是给了n个结点,让后让构成一个树,假设每个节点的度为r1, r2, ...rn,求f(x ...

随机推荐

  1. 使用 Xcode 5 生成和使用静态库

      本文转载至 http://blog.csdn.net/qq331436155/article/details/18363267   静态库Static Libraryiosxcode   在项目中 ...

  2. 【BZOJ2466】[中山市选2009]树 树形DP

    [BZOJ2466][中山市选2009]树 Description 图论中的树为一个无环的无向图.给定一棵树,每个节点有一盏指示灯和一个按钮.如果节点的按扭被按了,那么该节点的灯会从熄灭变为点亮(当按 ...

  3. An Ordinary Game(简单推导)

    An Ordinary Game Time limit : 2sec / Memory limit : 256MB Score : 500 points Problem Statement There ...

  4. web.xml配置中的log4jRefreshInterval

    采用spring框架的项目如何使用log4j在spring中使用log4j,有些方便的地方, 1.动态的改变记录级别和策略,即修改log4j.properties,不需要重启web应用,这需要在web ...

  5. java的小知识点

    1 获取当前路径 System.getProperty("user.dir") System.getProperty()参数大全# java.version            ...

  6. Moore-Penrose Matrix Inverse 摩尔-彭若斯广义逆 埃尔米特矩阵 Hermitian matrix

    http://mathworld.wolfram.com/Moore-PenroseMatrixInverse.html 显然,埃尔米特矩阵主对角线上的元素都是实数的,其特征值也是实数.对于只包含实数 ...

  7. Linux c编程:线程属性

    前面介绍了pthread_create函数,并且当时的例子中,传入的参数都是空指针,而不是指向pthread_attr_t结构的指针.可以使用pthread_attr_t结构修改线程默认属性,并把这些 ...

  8. lzugis—搭建属于自己的小型的版本号控制SVN

    版权声明:本文为LZUGIS原创文章,未经同意不得转载. https://blog.csdn.net/GISShiXiSheng/article/details/28643575 对于不了解SVN的同 ...

  9. R语言图形base系统(一)

           一般R作图有三大绘图系统:base系统.ggplot2绘图系统.lattice绘图系统.        本篇主要介绍base系统绘图时的图形参数.一般用plot()函数来完成.在R中,若 ...

  10. python 的for else语句

    for中间不是break出来的,是正常循环完跳出循环的会执行else内的语句 while else语句也是如此 这个以前的常见语言没有,特此记录