POJ2975 Nim

Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u

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 npiles 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:

 11110111101

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

Stanford Local 2005

这次是经典的nim问题,求先手若要必胜,有多少堆石头可以取。

若先手可以取走一些石头,使得剩下每堆的石头数异或和为0,那么算一种可行的方法。

首先若是先手可以必胜,那么所有堆石头数异或和s==0。若要从第i堆中取,使之后异或和为0,那么需要取s^a[i]堆((s^a[i])^a[i]==s==0)

若石头数量a[i]>(s^a[i]),就可以取。

另外要注意异或优先级比小于号低,需要加括号

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cmath>
 5 #include<cstring>
 6 using namespace std;
 7 int n;
 8 int a[1200],s,ans;
 9 int main(){
10     while(scanf("%d",&n) && n){
11         int i,j;
12         s=0;ans=0;
13         for(i=1;i<=n;i++)
14             scanf("%d",&a[i]),s^=a[i];
15         for(i=1;i<=n;i++){
16             if((s^a[i])<a[i]) ans++;
17         }
18         printf("%d\n",ans);
19     }
20     return 0;
21 }
时间: 2024-10-15 00:16:11

POJ2975 Nim的相关文章

poj2975 Nim(经典博弈)

Nim Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5866   Accepted: 2777 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 mor

POJ2975|Nim|博弈论

DescriptionNim 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

poj2975 Nim 博弈

      自从省赛结束了,好久都做过博弈题了,感觉都快忘了.今天找了几题练练手,在做过程中,感觉这道题挺有意思的.题目的意思是说,在Nim游戏中,先手有几种方式让 Nim 和变为0.(不知道Nim游戏的,请参考:这里) 其实我觉得这道题就是披着博弈的外衣,然后来考查你异或运算符(^)的使用的.在做题之前,我们想要了解异或运算符(^)的一个重要的性质: 现在我们有三个整数a, b, c: 我们假设c = a ^ b, 那么我们可以得到 b = c ^ a, a = b ^ c(交换律对异或运

poj2975(nim游戏取法)

求处于必胜状态有多少种走法. if( (g[i]^ans) <= g[i]) num++; //这步判断很巧妙 // // main.cpp // poj2975 // // Created by New_Life on 16/8/12. // Copyright © 2016年 chenhuan001. All rights reserved. // #include <iostream> #include <stdio.h> #include <string.h&g

POJ2975 Nim 博弈论 尼姆博弈

http://poj.org/problem?id=2975 题目始终是ac的最大阻碍. 问只取一堆有多少方案可以使当前局面为先手必败. 显然由尼姆博弈的性质可以知道需要取石子使所有堆石子数异或和为0,那么将某一堆a个石子变为a^异或和即可. a1^a2^a3^...^an=y; a1^a2^a3^...^an^y=0; 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<

Nim游戏

目前有3堆石子,每堆石子个数也是任意的,双方轮流从中取出石子,规则如下:1)每一步应取走至少一枚石子:每一步只能从某一堆中取走部分或全部石子:2)如果谁不能取谁就失败. Bouton定理: 必败状态当且仅当x1^x2^x3==0 SG函数和SG定理: 对于任意状态x,SG(x)=mex(S),S是x后继状态中SG函数值集合,mex(S)表示不在S内的最小非负整数 有这样一个游戏,是多个游戏共同进行,每个游戏都执行到底时才算整个游戏结束,每次一个选手可以把一个游戏进行一步. 对于这样的游戏它的某状

组合博弈入门

人生有“三晃”:一晃大了,一晃老了,一晃没了.我晃一下就够了... 以下为网上搜集资料的汇总: 组合游戏定义:        1.有且仅有两个玩家    2.游戏双方轮流操作    3.游戏操作状态是个有限的集合(比如:取石子游戏,石子是有限的,棋盘中的棋盘大小的有限的)  4.游戏必须在有限次内结束  5.当一方无法操作时,游戏结束. (一)巴什博奕(Bash Game):只有一堆n个物品,两个人轮流从这堆物品中取物,规定每次至少取一个,最多取m个.最后取光者得胜.     显然,如果n=m+

【POJ2975】Nim 博弈

转载请注明出处:http://blog.csdn.net/vmurder/article/details/42610939 其实我就是觉得原创的访问量比未授权盗版多有点不爽233... 题意:多组数据. 问先手有多少种取法使自己必胜. 题解: 首先Nim游戏的异或思想就不说了. 然后我们发现对于某个数,如果其它数的异或和比它小,那么先手拿掉这个数的一部分就可以使所有数异或为0, 先求出所有数异或和,然后再枚举每个数,异或一下就相当于其它所有数的异或和了, 然后可以进行判断出解. 长姿势: ^的优

[poj2975]Nim_博弈论

Nim poj-2975 题目大意:给定n堆石子,问:多少堆石子满足操作之后先手必胜. 注释:$1\le n\le 10^3$. 想法: 我们设M=sg(x1)^sg(x2)^...^sg(xn).其中,xi是第i堆石子个数. 如果sg(xi)^M<sg(xi),显然这堆石子满足题意. 最后,附上丑陋的代码... ... #include <iostream> #include <cstdio> #include <cstring> #include <al