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

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 k1, k2, …, 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:

 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

3
7 11 13
2
1000000000 1000000000
0

Sample Output

3
0

Source

【思路】

博弈

设v=a1^a2…^an,对于i,如果ai>(v^ai)则先手可以把ai变为v^ai,此时局面必败。累计ans即可。

【代码】

 #include<cstdio>
using namespace std; int n,a[]; int main() {
while(scanf("%d",&n)== && n) {
int v=,ans=;
for(int i=;i<n;i++)
scanf("%d",&a[i]) , v^=a[i];
for(int i=;i<n;i++)
if((v^a[i])<a[i]) ans++;
printf("%d\n",ans);
}
return ;
}

poj 2975 Nim(博弈)的更多相关文章

  1. POJ 2234 Nim博弈

    思路: nim博弈裸题 xor一下 //By SiriusRen #include <cstdio> using namespace std; int n,tmp,xx; int main ...

  2. POJ 2975 Nim 尼姆博弈

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

  3. POJ 2975 Nim(博弈)题解

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

  4. POJ 2975 Nim(博弈论)

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

  5. poj -2975 Nim

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

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

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

  7. poj 2975 Nim 博弈论

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

  8. POJ 2975 Nim(普通nim)

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

  9. POJ 2234 Matches Game(Nim博弈裸题)

    Description Here is a simple game. In this game, there are several piles of matches and two players. ...

随机推荐

  1. [Mime] MimeReader--读取Mime的帮助类 (转载)

    点击下载 MimeReader.rar 这个类是关于MimeReader的帮助类看下面代码吧 /// <summary> /// 类说明:Assistant /// 编 码 人:苏飞 // ...

  2. TCP服务器端和客服端(一)

    就是一个客服端(Socket)和服务器(ServerSocket)端的链接间.我的理解是一个服务端可以链接多个客服端. 在客服端有输入流outPutStream. 用于发送数据 在服务器端有输出流.i ...

  3. 【html】【3】html标签列表

    必看参考: http://www.divcss5.com/html/h323.shtml http://www.w3school.com.cn/tags/tag_html.asp 常用: <ht ...

  4. supervisor进程管理

    install :  apt-get install supervisor crete  a  xxxx.conf  file at   /etc/supervisor/conf.d the cont ...

  5. ntpath join(path, *paths) 发生UnicodeDecodeError的Bug的解决方案

    最近在学习用Python开发web,使用的是web框架,在win8.1平台下安装SAE和Flask的时候总是在ntpath.py这个UnicodeDecodeError模块报错. 84行 result ...

  6. 学习总结之Log4NET

    通过在网上查找了一些资料,用了些时间学习了log4NET,做了一个小小的总结,说一下它的特点吧 首先呢log4NET是.Net下一个非常优秀的开源日志记录组件.它可以将日志分成不同等级,也可以按照我们 ...

  7. [译]36 Days of Web Testing(三)

    Day 14: Automate the tedious Why ? 有些时候,web测试还是蛮单调乏味的,在开始测试前,你可能要必须跳转到一个特定的表单页面,或则为了得到一个特定的页面(或配置),你 ...

  8. 检测目标程序ELF bit是32还是64

    android操作系统在5.0之后加入了对64位程序的支持,同时兼容运行32位的进程 android的进程绝大部分是zygote父进程fork出来的子进程 zygote进程fork出来的进程是32位进 ...

  9. [BZOJ 1901] Dynamic Rankings 【树状数组套线段树 || 线段树套线段树】

    题目链接:BZOJ - 1901 题目分析 树状数组套线段树或线段树套线段树都可以解决这道题. 第一层是区间,第二层是权值. 空间复杂度和时间复杂度均为 O(n log^2 n). 线段树比树状数组麻 ...

  10. Unity3d 物理 Rigidbody预防穿插

    Unity3d 物理 Rigidbody预防穿插 @广州小龙 Unity的物理引擎采用的是Physx引擎,里面的组件是Rigidbody!这个可以模拟物理效果! Rigidbody,这个是可以进行穿插 ...