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:

 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
第3种博弈,第一个赢,有几种方法。
#include<stdio.h>
int main()
{
int n,i,count,k,t;
int a[1000];
while(~scanf("%d",&n)&&n!=0)
{
count=t=0;
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
t=t^a[i];
}
if(t!=0)
{
for(i=0;i<n;i++)
{
k=t^a[i];
if(k<a[i])
count++;
}
}
printf("%d\n",count);
}
return 0;
}
 

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. json 是什么

    怎么学习一个知识? 首先要提出几个问题,目前认为json是个什么,json是谁创造的,为什么而出现的,但是目前仅有很长时间之前别人直接告诉我的,json用来存数据的,对于使用也忘记的差不多了,所以现在 ...

  2. [DEncrypt] HashEncode--哈希加密帮助类 (转载)

    点击下载 HashEncode.zip 这个类是关于加密,解密的操作,文件的一些高级操作1.HashEncode 得到随机哈希加密字符串2.HashEncode 得到一个随机数值3.HashEncod ...

  3. C#当中的多线程_线程基础

    前言 最近工作不是很忙,想把买了很久了的<C#多线程编程实战>看完,所以索性把每一章的重点记录一下,方便以后回忆. 第1章 线程基础 1.创建一个线程 using System; usin ...

  4. c语言学习之基础知识点介绍(六):if和switch结构

    本节主要说c语言中的分支结构. c语言中分支结构有两大类:一种是if,一种是switch. 一.if结构 //if结构,语法: //第一种: if(表达式){ //语句块 } //第二种: if(表达 ...

  5. c语言学习之基础知识点介绍(二):格式化控制符和变量的补充

    上节简单介绍了c语言中的一些基础知识点,本节将对之前介绍的不够详细的知识点进行补充. 格式化控制符的消息介绍: %d的其他控制符: 1.%md:m代表这个整数位占用多少位,m是一个整数.实际数字不足的 ...

  6. Tomcat-java.lang.NoClassDefFoundError: org/apache/juli/logging/LogFactory

    好些天没弄java了,今天开MyEclipse,发现启动Tomcat的时候发错了,后来发现,报错如题. 解决方案是将 bin/tomcat-juli.jar 添加到add tomcat classpa ...

  7. .net下载文件方法

    1.以文件流下载 byte[] fileStr=new byte[5]; MemoryStream btMs = new MemoryStream(fileStr); //以字符流的形式下载文件 by ...

  8. html-----005

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. cocos2dx系列笔记(1)- windows环境配置前篇

    cocos2dx升级之旅,请多指教~ 本篇是本人搭建cocos2dx-Windows 64位环境的配置说明,仅供参考. 开发准备 搭建环境肯定需要准备好所有工具,只有把工具都准备好了,才能撸起袖子干活 ...

  10. HTML5 程序设计笔记(一)

    HTML5 概述 1.html5 发展史 1993年html首次以因特网草案形式发布. 20世纪90年代,html大幅发展,从2.0版,到3.2版和4.0版.最后到1999年的4.01版. 伴随htm ...