poj 2975 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(博弈)的更多相关文章
- POJ 2234 Nim博弈
思路: nim博弈裸题 xor一下 //By SiriusRen #include <cstdio> using namespace std; int n,tmp,xx; int main ...
- POJ 2975 Nim 尼姆博弈
题目大意:尼姆博弈,求先手必胜的情况数 题目思路:判断 ans=(a[1]^a[2]--^a[n]),求ans^a[i] < a[i]的个数. #include<iostream> ...
- POJ 2975 Nim(博弈)题解
题意:已知异或和为0为必败态,异或和不为0为必胜态,问你有几种方法把开局从当前状态转为必败态. 思路:也就是说,我们要选一堆石头,然后从这堆石头拿走一些使剩下的石碓异或和为0.那么只要剩下石堆的异或和 ...
- POJ 2975 Nim(博弈论)
[题目链接] http://poj.org/problem?id=2975 [题目大意] 问在传统的nim游戏中先手必胜策略的数量 [题解] 设sg=a1^a1^a3^a4^………^an,当sg为0时 ...
- poj -2975 Nim
Nim Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4312 Accepted: 1998 Description ...
- [原博客] POJ 2975 Nim 统计必胜走法个数
题目链接题意介绍了一遍Nim取石子游戏,可以看上一篇文章详细介绍.问当前状态的必胜走法个数,也就是走到必败状态的方法数. 我们设sg为所有个数的Xor值.首先如果sg==0,它不可能有必胜走法,输出0 ...
- poj 2975 Nim 博弈论
令ans=a1^a2^...^an,如果需要构造出异或值为0的数, 而且由于只能操作一堆石子,所以对于某堆石子ai,现在对于ans^ai,就是除了ai以外其他的石子 的异或值,如果ans^ai< ...
- POJ 2975 Nim(普通nim)
题目链接 #include<iostream> #include<cstdio> using namespace std; int main() { ]; int sum,cn ...
- POJ 2234 Matches Game(Nim博弈裸题)
Description Here is a simple game. In this game, there are several piles of matches and two players. ...
随机推荐
- 在Android上模拟登录广工正方教务系统查询成绩
这是在博客园里开博以来写的第一篇博客. 因为之前看过很多人都有发过关于模拟登录正方软件获取数据的文章,自己觉得挺好玩的便也去动手一做,开始还以为挺难的,但实际做起来还蛮简单的,当然其中还有些小插曲. ...
- 在mipsel-linux平台上的编译应用SQLite-3.5.9
sqlite 第一个Alpha版本诞生于2000年5月,是实现了SQL 92标准的一个大子集的嵌入式数据库,其以在一个库中组合了数据库引擎和接口,能将所有数据存储于单个文件中.官方测试表明sqlite ...
- javascript创建对象(一)
对象定义:无序属性的集合,属性包含基本值.对象.函数,相当于一组没有特定顺序的值. 创建自定义对象最简单的方式就是: var movie=new Object(); movie.name=&qu ...
- ios 将状态栏改为白色方法!
1在Info.plist中设置UIViewControllerBasedStatusBarAppearance 为NO2 在需要改变状态栏颜色的ViewController中在ViewDidLoad方 ...
- 原始的JDBC操作
-----------------------------根据配置文件---------------------------- package cn.gdpe.jdbc; import java.io ...
- Action配置
Action是一个逻辑控制器,并不直接对浏览器生成响应,而是返回指定逻辑视图(一个字符串). 不推荐在Action的name属性值中使用点(.)和中划线(-),有可能会引发一些未知异常. 1使用A ...
- Struts2常规配置
默认配置文件名:struts.xml WEB-INF/classes下(放到src下) Struts2的有效常量可以查看 org\apache\struts2 下的 default.p ...
- SVN遇到Can't convert string from 'UTF-8' to native encoding
刚配好mysql,svn co代码的时候遇到问题 svn: Can't convert string from 'UTF-8' to native encoding: svn: platform/co ...
- 通过try、except和else的使用来使Python程序更加“强壮”
在执行的程序中,难免会碰到因为一些原因如输入输出导致致命性错误产生的情况(如因为输入的文件名错误而导致无法运行相关的代码.).此时你不希望程序直接挂掉,而是通过显示一些信息,使其平稳的结束.此时,就可 ...
- Centos6.4配置Fedora EPEL源附配置hop5.in源
查看系统版本 cat /etc/redhat-release 下载CentOS 版本所对应的EPEL 的版本 wget http://download.fedoraproject.org/pub/ep ...