C++-POJ2975-Nim
题目把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的更多相关文章
- poj2975 Nim 胜利的方案数
Nim Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5545 Accepted: 2597 Description N ...
- poj2975(nim游戏取法)
求处于必胜状态有多少种走法. if( (g[i]^ans) <= g[i]) num++; //这步判断很巧妙 // // main.cpp // poj2975 // // Created b ...
- POJ2975 Nim 【博弈论】
DescriptionNim is a 2-player game featuring several piles of stones. Players alternate turns, and on ...
- POJ2975:Nim(Nim博弈)
Nim Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7279 Accepted: 3455 题目链接:http://p ...
- poj2975 Nim(经典博弈)
Nim Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5866 Accepted: 2777 Description N ...
- POJ2975 Nim 博弈论 尼姆博弈
http://poj.org/problem?id=2975 题目始终是ac的最大阻碍. 问只取一堆有多少方案可以使当前局面为先手必败. 显然由尼姆博弈的性质可以知道需要取石子使所有堆石子数异或和为0 ...
- [poj2975]Nim_博弈论
Nim poj-2975 题目大意:给定n堆石子,问:多少堆石子满足操作之后先手必胜. 注释:$1\le n\le 10^3$. 想法: 我们设M=sg(x1)^sg(x2)^...^sg(xn).其 ...
- [LeetCode] Nim Game 尼姆游戏
You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- HDU 5795 A Simple Nim 打表求SG函数的规律
A Simple Nim Problem Description Two players take turns picking candies from n heaps,the player wh ...
随机推荐
- 【python基础语法】第6天作业练习题
''' 二.作业(每一道题封装成一个函数) 1.输出99乘法表,结果如下:(提示嵌套for循环,格式化输出) 2.有1 2 3 4 这四个数字,设计程序计算能组成多少个互不相同且无重复数字的3位数?分 ...
- vim编辑超大文件
进入大文件(12g,250w+ lines),vim,耐心等待 有两种方法编辑删除冗余字段 1.set number ,可以通过:+数字组合跳到指定行,输入命令 ":100,200d&q ...
- C#实现图片文件到数据流,再到图片文件的转换
//----引入必要的命名空间 using System.IO; using System.Drawing.Imaging; //----代码部分----// private byte[] photo ...
- JavaSE学习笔记(9)---集合类和泛型
JavaSE学习笔记(9)---集合类和泛型 1.Collection集合 集合概述 在前面我们已经学习过并使用过集合ArrayList<E> ,那么集合到底是什么呢? 集合:集合是jav ...
- 剑指offer-面试题18-删除链表的节点-链表
/* 题目:给定单向链表的头指针和一个节点指针,定义一个函数在O(1)时间内删除该节点. */ /* 思路: 将要删除的节点的下一个节点的value和next复制过来,删除下一个节点. 考虑两种特殊情 ...
- Luogu2345 | 奶牛集会 (树状数组)
题目背景 MooFest, 2004 Open 题目描述 约翰的 \(N\) 头奶牛每年都会参加"哞哞大会".哞哞大会是奶牛界的盛事.集会上的活动很多,比如堆干草,跨栅栏,摸牛仔的 ...
- iframe刷新另一个iframe
如果是程序: Response.Write("<script language=javascript>"); Response.Write ...
- kali的virtualbox镜像账号密码
下载最新版本kali的virtualbox镜像 默认账号密码 root 密码 toor
- vue动态绑定图片和背景图
1.动态绑定图片 <img class="binding-img" :src="require('../assets/images/test.png')" ...
- Redis基础详解
1. Redis是什么.特点.优势 Redis是一个开源的使用C语言编写.开源.支持网络.可基于内存亦可持久化的日志型.高性能的Key-Value数据库,并提供多种语言的API. 它通常被称为 数据结 ...