Nim
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4312   Accepted: 1998

Description

Nim is a 2-player game featuring several piles of stones. Players alternate turns, and on his/her turn, a player’s move consists of removing one or more stones from any single pile. Play ends when all the stones have been removed, at which point the last player to have moved is declared the winner. Given a position in Nim, your task is to determine how many winning moves there are in that position.

A position in Nim is called “losing” if the first player to move from that position would lose if both sides played perfectly. A “winning move,” then, is a move that leaves the game in a losing position. There is a famous theorem that classifies all losing positions. Suppose a Nim position contains n piles having k1k2, …, kn stones respectively; in such a position, there are k1 + k2 + … + kn possible moves. We write each ki in binary (base 2). Then, the Nim position is losing if and only if, among all the ki’s, there are an even number of 1’s in each digit position. In other words, the Nim position is losing if and only if the xor of the ki’s is 0.

Consider the position with three piles given by k1 = 7, k2 = 11, and k3 = 13. In binary, these values are as follows:

  1. 111 1011 1101

There are an odd number of 1’s among the rightmost digits, so this position is not losing. However, suppose k3 were changed to be 12. Then, there would be exactly two 1’s in each digit position, and thus, the Nim position would become losing. Since a winning move is any move that leaves the game in a losing position, it follows that removing one stone from the third pile is a winning move when k1 = 7, k2 = 11, and k3 = 13. In fact, there are exactly three winning moves from this position: namely removing one stone from any of the three piles.

Input

The input test file will contain multiple test cases, each of which begins with a line indicating the number of piles, 1 ≤ n ≤ 1000. On the next line, there are n positive integers, 1 ≤ ki ≤ 1, 000, 000, 000, indicating the number of stones in each pile. The end-of-file is marked by a test case with n = 0 and should not be processed.

Output

For each test case, write a single line with an integer indicating the number of winning moves from the given Nim position.

Sample Input

  1. 3
  2. 7 11 13
  3. 2
  4. 1000000000 1000000000
  5. 0

Sample Output

  1. 3
  2. 0
    3种博弈,第一个赢,有几种方法。
  1. #include<stdio.h>
  2. int main()
  3. {
  4. int n,i,count,k,t;
  5. int a[1000];
  6. while(~scanf("%d",&n)&&n!=0)
  7. {
  8. count=t=0;
  9. for(i=0;i<n;i++)
  10. {
  11. scanf("%d",&a[i]);
  12. t=t^a[i];
  13. }
  14. if(t!=0)
  15. {
  16. for(i=0;i<n;i++)
  17. {
  18. k=t^a[i];
  19. if(k<a[i])
  20. count++;
  21. }
  22. }
  23. printf("%d\n",count);
  24. }
  25. return 0;
  26. }
  1.  

poj -2975 Nim的更多相关文章

  1. POJ 2975 Nim(博弈论)

    [题目链接] http://poj.org/problem?id=2975 [题目大意] 问在传统的nim游戏中先手必胜策略的数量 [题解] 设sg=a1^a1^a3^a4^………^an,当sg为0时 ...

  2. [原博客] POJ 2975 Nim 统计必胜走法个数

    题目链接题意介绍了一遍Nim取石子游戏,可以看上一篇文章详细介绍.问当前状态的必胜走法个数,也就是走到必败状态的方法数. 我们设sg为所有个数的Xor值.首先如果sg==0,它不可能有必胜走法,输出0 ...

  3. poj 2975 Nim(博弈)

    Nim Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5232   Accepted: 2444 Description N ...

  4. poj 2975 Nim 博弈论

    令ans=a1^a2^...^an,如果需要构造出异或值为0的数, 而且由于只能操作一堆石子,所以对于某堆石子ai,现在对于ans^ai,就是除了ai以外其他的石子 的异或值,如果ans^ai< ...

  5. POJ 2975 Nim(普通nim)

    题目链接 #include<iostream> #include<cstdio> using namespace std; int main() { ]; int sum,cn ...

  6. POJ 2975 Nim 尼姆博弈

    题目大意:尼姆博弈,求先手必胜的情况数 题目思路:判断 ans=(a[1]^a[2]--^a[n]),求ans^a[i] < a[i]的个数. #include<iostream> ...

  7. POJ 2975 Nim(博弈)题解

    题意:已知异或和为0为必败态,异或和不为0为必胜态,问你有几种方法把开局从当前状态转为必败态. 思路:也就是说,我们要选一堆石头,然后从这堆石头拿走一些使剩下的石碓异或和为0.那么只要剩下石堆的异或和 ...

  8. poj 2975 Nim_最经典的Nim取石子

    题意:给你n堆石头,每次只能在一堆取最少一个石子,最后拿走最后一堆的为胜者,问胜者有多少种赢得取法 #include <iostream> #include<cstdio> u ...

  9. HDU 3404&POJ 3533 Nim积(二维&三维)

    (Nim积相关资料来自论文曹钦翔<从"k倍动态减法游戏"出发探究一类组合游戏问题>) 关于Nim积计算的两个函数流程: 代码实现如下: ][]={,,,}; int N ...

随机推荐

  1. plsql中文乱码问题(显示问号)

    问题:打开plsql,执行sql语句,中文显示乱码: 解决方案: 输入sql语句select userenv('language') from dual查看数据库字符集 输入sql语句select * ...

  2. GridView分页的实现

    要在GridView中加入 //实现分页 AllowPaging="true" //一页数据10行 PageSize="10" // 分页时触发的事件 OnPa ...

  3. 什么是WordPress?

    (今天由于好友 肖知虎的 的需求 , 我开始了帮助小虎建站的需求, 就这样开始学习了Wordpress.  这些文章就是为了记录这些我在学习过程当中的心得,和记录下来的文字而已) 什么是WordPre ...

  4. 忘记Mysql的root密码怎么办?

    解决方法: 1.打开cmd,用net start命令查看是否开启了mysql服务,如果开启,用net stop mysql 命令关闭mysql 2.进入mysql的安装目录下的bin目录,例如:E:\ ...

  5. [Linux]XAMPP安装

    XAMPP安装下载地址:http://xiazai.zol.com.cn/index.php?c=Detail_DetailMini&n=19e18f86d0596b5cd&softi ...

  6. strtotime 的几点不同

    在php里面,strtotime()有点比较特殊,date函数也有点问题 if ( date(1,time()) == "Monday")    //似乎 date(1,time( ...

  7. AppDomain与进程、线程、Assembly之间关系

    AppDomain是CLR的运行单元,它可以加载Assembly.创建对象以及执行程序 AppDomain是CLR实现代码隔离的基本机制.   每一个AppDomain可以单独运行.停止:每个AppD ...

  8. Memcache,Redis

    Memcache Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度. ...

  9. VS下面的编译错误-----转换到 COFF 期间失败: 文件无效或损坏

    最近写了一个vs的小项目,然后编译的时候vs提示了"转换到 COFF 期间失败: 文件无效或损坏"的问题. 去网上搜索了一个解决方案.原作者的链接是:http://jingyan. ...

  10. URL encode 与 URL decode 的C语言实现

    转载自:http://blog.csdn.net/langeldep/article/details/6264058 本文代码为从PHP代码中修改而来,只保留了2个函数. int php_url_de ...