题目把Nim游戏为什么可以取异或和讲解得十分清楚,建议多读几次,理解一下

再一个,可以把每次异或视为一次取数,因此(k[i]^sg)<k[i]即为一种可行操作

/*
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.
*/
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int n,k[];
int main(){
while(scanf("%d",&n)){
if(n==)break;
int sg=,ans=;
for(int i=;i<=n;i++)scanf("%d",&k[i]),sg^=k[i];
for(int i=;i<=n;i++)if((k[i]^sg)<k[i])ans++;
printf("%d\n",ans);
}
return ;
}

C++-POJ2975-Nim的更多相关文章

  1. poj2975 Nim 胜利的方案数

    Nim Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5545   Accepted: 2597 Description N ...

  2. poj2975(nim游戏取法)

    求处于必胜状态有多少种走法. if( (g[i]^ans) <= g[i]) num++; //这步判断很巧妙 // // main.cpp // poj2975 // // Created b ...

  3. POJ2975 Nim 【博弈论】

    DescriptionNim is a 2-player game featuring several piles of stones. Players alternate turns, and on ...

  4. POJ2975:Nim(Nim博弈)

    Nim Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7279   Accepted: 3455 题目链接:http://p ...

  5. poj2975 Nim(经典博弈)

    Nim Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5866   Accepted: 2777 Description N ...

  6. POJ2975 Nim 博弈论 尼姆博弈

    http://poj.org/problem?id=2975 题目始终是ac的最大阻碍. 问只取一堆有多少方案可以使当前局面为先手必败. 显然由尼姆博弈的性质可以知道需要取石子使所有堆石子数异或和为0 ...

  7. [poj2975]Nim_博弈论

    Nim poj-2975 题目大意:给定n堆石子,问:多少堆石子满足操作之后先手必胜. 注释:$1\le n\le 10^3$. 想法: 我们设M=sg(x1)^sg(x2)^...^sg(xn).其 ...

  8. [LeetCode] Nim Game 尼姆游戏

    You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...

  9. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  10. HDU 5795 A Simple Nim 打表求SG函数的规律

    A Simple Nim Problem Description   Two players take turns picking candies from n heaps,the player wh ...

随机推荐

  1. 洛谷题解 P1292 【倒酒】

    原题传送门 题目描述 Winy是一家酒吧的老板,他的酒吧提供两种体积的啤酒,a ml和b ml,分别使用容积为a ml和b ml的酒杯来装载. 酒吧的生意并不好.Winy发现酒鬼们都非常穷.有时,他们 ...

  2. Linux物理磁盘扩容流程

    1. 插入硬盘前,查看现有硬盘情况 (1)命令:fdisk -l 说明:fdisk -l 查看设备的所有分区 (2)命令:df -h 说明:df 列出文件系统的整体磁盘使用量 2. 断电插入硬盘后,重 ...

  3. Python爬虫连载3-Post解析、Request类

    一.访问网络的两种方法 1.get:利用参数给服务器传递信息:参数为dict,然后parse解码 2.post:一般向服务器传递参数使用:post是把信息自动加密处理:如果想要使用post信息,需要使 ...

  4. Spring Aop和Spring Ioc(一)

    Spring Aop Aop: 面向切面编程的本质:面向切面编程,指扩展功能不修改源代码,将功能代码从业务逻辑代码中分离出来. 1:主要功能:日志记录,性能统计,安全控制,事务处理,异常处理等等. 2 ...

  5. 【Debian学徒记事】Debian快速呼出Terminal终端

    Debian快速呼出Terminal终端 书接上回,Debian已经安装完毕 失踪的Ctrl+Alt+T 安装完毕启动,我发现了剑很诡异的事,Ctrl+Alt+T居然失灵了 (在多次测试后发现,Deb ...

  6. Python 测试代码 初学者笔记

    单元测试 每完成一个单元测试,Python都会打印一个字符: 测试通过打印一个句点:测试引发错误打印E:测试导致断言失败打印F 模块unittest import unittest from name ...

  7. Python 变量&列表 初学者笔记

    变量 消除空白后该变量需要存储一下(此操作常用于“储存用户输入并对其进行清理”) strip()消除两端空白 lstrip()消除前部空白 rstrip()消除末尾空白   upper()全部字母大写 ...

  8. 超级丑数--用查找的api解决

    质数又称素数.一个大于1的自然数,除了1和它自身外,不能被其他自然数整除的数叫做质数:否则称为合数. 质因数(素因数或质因子)在数论里是指能整除给定正整数的质数.除了1以外,两个没有其他共同质因子的正 ...

  9. Bonny校园app使用体验

    Bonny校园是一款集校园表白墙.失物招领处和二手市场集一体的一款校园app,旨在帮助大学生解决校内的生活问题.这款app功能比较齐全,表白墙内含有许多有趣的信息展示,失物招领处内可以详细的展示捡到东 ...

  10. 如何在Mac OS X上将PDF转换为Microsoft Word

    Lighten PDF to Word Converter for Mac是一个简单但功能强大的应用程序,可将PDF文件准确,轻松地转换为Microsoft Word.它可以保留原始内容的布局,格式, ...