Revenge of Nim II

Problem Description
Nim is a mathematical game of strategy in which two players take turns removing objects from distinct heaps. On each turn, a player must remove at least one object, and may remove any number of objects provided they all come from the same heap.
---Wikipedia
Today, Nim takes revenge on you, again. As you know, the rule of Nim game is rather unfair, only the nim-sum (⊕) of the sizes of the heaps is zero will the first player lose. To ensure the fairness of the game, the second player has a chance to move some (can be zero) heaps before the game starts, but he has to move one heap entirely, i.e. not partially. Of course, he can’t move all heaps out, at least one heap should be left for playing. Will the second player have the chance to win this time?
Input
The first line contains a single integer T, indicating the number of test cases.
Each test case begins with an integer N, indicating the number of heaps. Then N integer Ai follows, indicating the number of each heap.
[Technical Specification]
1. 1 <= T <= 100
2. 1 <= N <= 1 000
3. 1 <= Ai <= 1 000 000 000 000
Output
For each test case, output “Yes” if the second player can win by moving some (can be zero) heaps out, otherwise “No”.
Sample Input
3
1
2
3
2 2 2
5
1 2 3 4 5
Sample Output
No
Yes
Yes

题目大意:

    Nim的复仇。有N堆石子,后手可以在游戏开始前拿走任意堆石子再进行Nim博弈,问后手是否有机会赢。

解题目标:

    先来复习一下Nim博弈。

    给定N堆石子,假设第i堆石子有a[i]个石头子,两个人轮流从某一堆中取任意多的物品,规定每一次至少取一个,多者不限,最后取光者胜。

    Nim博弈的常规解法:k=a[1]^a[2]^a[3]^....^a[n]

      若k==0 先手必输

      若k!=0  后手必输

    显然这个题的目标就是 找出石子堆集合的一个子集(子集元素个数大于等于2),使其元素异或之后为0即可。

解题思路:

    错误思路:

        做比赛的时候,想着用随机化算法来水过去,循环了350W次,提交了3遍。。 发现N<=1000,随机化算法不可能过。。

    正确思路:

        大牛的blog就是不一般。思路就一两句话,研究了整整一天,才勉强看懂。(一大牛题解就三个字:位运算。。Orz)

        根据官方题解的思路来看,要把这些数字看成一个二进制矩阵

        该矩阵具有一下性质:

        (1)每一个空都是1或0

        (2)相对于只有1或0两个值的矩阵元素,那么异或运算相当于其加减法:1+1=0  1+0=1  0+1=1  0+0=0  1-1=0  1-0=1  0-1=1  0-0=0

        再根据矩阵的秩与极大无关组的相关定理可知:若矩阵的秩rank<N,那么极大无关组的成员数要小于N,那么对于任意一个a[i](特别对于不属于极大无关组的a[i])都可以用极大无关组中的成员来进行表示。

        在根据性质:对于任意数a,a^a=0可知,当且仅当rank<N时,存在一个子集使其异或值为0。

解题方法:

    据说是高斯消元。将矩阵化成梯形,那么存在空行,即rank<N。

    ps:前面说了,异或在二进制中相当于加减。与矩阵的初等变化不矛盾。

Code:

  1. /*************************************************************************
  2. > File Name: BestCode#16_1003.cpp
  3. > Author: Enumz
  4. > Mail: 369372123@qq.com
  5. > Created Time: 2014年11月01日 星期六 17时44分16秒
  6. ************************************************************************/
  7.  
  8. #include<iostream>
  9. #include<cstdio>
  10. #include<cstdlib>
  11. #include<string>
  12. #include<cstring>
  13. #include<list>
  14. #include<queue>
  15. #include<stack>
  16. #include<map>
  17. #include<set>
  18. #include<algorithm>
  19. #include<cmath>
  20. #include<bitset>
  21. #include<time.h>
  22. #include<climits>
  23. #define MAXN 3000
  24. using namespace std;
  25. long long a[MAXN];
  26. int main()
  27. {
  28. int T;
  29. cin>>T;
  30. while (T--)
  31. {
  32. int N;
  33. cin>>N;
  34. for (int i=;i<=N;i++)
  35. scanf("%I64d",&a[i]);
  36. int row=,col=;
  37. for ( ;row<=N&&col<=;col++,row++) //有数据范围可知,化成二进制之后,列数不会超过40
  38. {
  39. int tmp_row;
  40. for (tmp_row=row;tmp_row<=N;tmp_row++) //找到当前列下的第一个不为零的值
  41. if (a[tmp_row]&(1LL<<col)) //判断是否为1
  42. break;
  43. if (tmp_row==N+) //不存在为1的点,说明存在一个空行,rank--
  44. {
  45. row--;
  46. continue;
  47. }
  48. swap(a[tmp_row],a[row]); //交换行
  49. for (int i=tmp_row+;i<=N;i++)
  50. if (a[i]&(1LL<<col))
  51. a[i]^=a[row];
  52. }
  53. if (row<N)
  54. printf("Yes\n");
  55. else
  56. printf("No\n");
  57. }
  58. return ;
  59. }

    

HDU5088——Revenge of Nim II(高斯消元&矩阵的秩)(BestCoder Round #16)的更多相关文章

  1. 【bzoj3105】[cqoi2013]新Nim游戏 高斯消元求线性基

    题目描述 传统的Nim游戏是这样的:有一些火柴堆,每堆都有若干根火柴(不同堆的火柴数量可以不同).两个游戏者轮流操作,每次可以选一个火柴堆拿走若干根火柴.可以只拿一根,也可以拿走整堆火柴,但不能同时从 ...

  2. BZOJ 3105: [cqoi2013]新Nim游戏 [高斯消元XOR 线性基]

    以后我也要用传送门! 题意:一些数,选择一个权值最大的异或和不为0的集合 终于有点明白线性基是什么了...等会再整理 求一个权值最大的线性无关子集 线性无关子集满足拟阵的性质,贪心选择权值最大的,用高 ...

  3. BZOJ 3569: DZY Loves Chinese II [高斯消元XOR 神题]

    http://www.lydsy.com/JudgeOnline/problem.php?id=3569 题意:多次询问一个无向连通图当图中某k条边消失时这个图是否联通 强制在线 太神啦啦啦啦啦啦啦啦 ...

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

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

  5. POJ 3185 The Water Bowls (高斯消元)

    题目链接 题意:翻译过来就是20个0或1的开关,每次可以改变相邻三个的状态,问最小改变多少次使得所有开关都置为0,题目保证此题有解. 题解:因为一定有解,所以我们可以正序逆序遍历两次求出较小值即可.当 ...

  6. A - The Water Bowls POJ - 3185 (bfs||高斯消元)

    题目链接:https://vjudge.net/contest/276374#problem/A 题目大意:给你20个杯子,每一次操作,假设当前是对第i个位置进行操作,那么第i个位置,第i+1个位置, ...

  7. Codeforces 832E Vasya and Shifts - 高斯消元

    题目传送门 快速的传送门I 快速的传送门II 题目大意 (题意比较复杂,请自行阅读原题) 可以将原题的字母都看成它们的在字符表中的下标,这样问题就变成给定$n$个$m$维向量$\vec{a_{1}}, ...

  8. hdu 3915 高斯消元

    http://acm.hdu.edu.cn/showproblem.php?pid=3915 这道题目是和博弈论挂钩的高斯消元.本题涉及的博弈是nim博弈,结论是:当先手处于奇异局势时(几堆石子数相互 ...

  9. 洛谷P3389 高斯消元 / 高斯消元+线性基学习笔记

    高斯消元 其实开始只是想搞下线性基,,,后来发现线性基和高斯消元的关系挺密切就一块儿在这儿写了好了QwQ 先港高斯消元趴? 这个算法并不难理解啊?就会矩阵运算就过去了鸭,,, 算了都专门为此写个题解还 ...

随机推荐

  1. WCF全面解析第二章 地址(Adress)

    2.1 统一资源标识(URL) 2.1.1 Http/Https 2.1.2 Net.TCP 2.1.3 Net.Pipe WCF只将命名管道专门用于同一台机器的跨进程通信. 2.1.4 Net.Ms ...

  2. oracle创建用户,修改用户,删除用户等关于用户的

    --直接修改底层表 USER$ 更换用户名 1.windows 平台下运行 cmd 2.sqlplus /nolog 3.SQL> conn SYSTEM/123@ORCL as sysdba ...

  3. 插值和空间分析(一)_探索性数据分析(R语言)

    > library(lattice) > library(sp) > data(meuse) > coordinates(meuse) <- c("x" ...

  4. 设计模式之单例模式(Singleton Pattern)

    单例模式 单例模式(Singleton Pattern)在java中算是最常用的设计模式之一,主要用于控制控制类实例的数量,防止外部实例化或者修改.单例模式在某些场景下可以提高系统运行效率.实现中的主 ...

  5. nginx学习之一

    http://tengine.taobao.org/book/chapter_02.html

  6. 与MySQL交互(felixge/node-mysql)

    目录 简介和安装 测试MySQL 认识一下Connection Options MYSQL CURD 插入 更新 查询 删除 Nodejs 调用带out参数的存储过程,并得到out参数返回值 结束数据 ...

  7. 第一章 Web MVC简介

    Web MVC简介 1.1.Web开发中的请求-响应模型: 在Web世界里,具体步骤如下: 1.  Web浏览器(如IE)发起请求,如访问hao123主页 2.  Web服务器(如Tomcat)接收请 ...

  8. 包装设计模式的实现以改进BufferedReader中的readLine方法为例

    实现与目标对象相同的接口     BufferedReader 定义一个变量记住目标对象 定义一个构造器接收被增强对象 覆盖需要增强的方法 对于不想增强的方法,直接调用目标对象的方法. package ...

  9. Android-Empty-Layout:展示不同类型的页面布局,用于视图是空的时候

    Android-Empty-Layout:这个布局可以作用在Listview,Gridview,用于显示数据的是空的时候,可以提示友好的页面.这库可以显示页面出错,页面加载,页面是空. 加载的动画页面 ...

  10. Python Socket File Transfer

    I have a RPi which I intented to use it to crawl data. The development environment in RPi is very ba ...