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 ...
随机推荐
- 硬件知识整理part3--电阻在单片机系统中的应用
邦有道,如矢:邦无道,如矢. --孔子 电阻在电路中主要功能是限流和分压等等.在单片机系统中自然也是. 电阻作为限流应该是最常用的应用之一,对于单片机外围设计来说,电阻的应用非常重要,在很多时候,我 ...
- 【gRPC】如何便捷的调试gRPC程序
前言 gRPC是一款广泛应用的rpc框架,因为基于C/S架构,服务启动之后,需要编写对应的客户端才能调用,调试起来相对麻烦一些,这里主要介绍一下如何通过swagger-ui来调试grpc服务. grp ...
- 【spring boot】SpringBoot初学(8)– 简单整合redis
前言 到目前为止,把项目中需要用到的:properties读取.数据源配置.整合mybatis/JdbcTemplate.AOP.WebService.redis.filter.interceptor ...
- wondiws+centos 双系统
任务:在windows下安装CentOS 1.下载CentOS镜像文件,准备一个未格式化的空间. 2.使用UltraISO将要安装的系统写入U盘. 3.用U盘启动,将系统装入一个空的分区下(未格式化) ...
- 剑指offer-面试题53_3-数组中数值和下标相等的元素-二分查找
/* 题目: 求单调递增数组中,数值与下标相等的任意数字. */ /* 思路: 二分法. */ #include<iostream> #include<cstring> #in ...
- 【Android】java中调用JS的方法
最近因为学校换了新的教务系统,想做一个模拟登陆功能,发现登陆的账号和密码有一个js脚本来进行加密 整理了一下java中执行JS的方法 智强教务 账号 密码 加密方法 var keyStr = &quo ...
- 破解版 Teamver 安装
一 .下载安装包 百度网盘链接:https://pan.baidu.com/s/18nEKAMmHEqU66Dq_aCnEYQ 提取码:2x2q 二.解压缩后,直接运行红框内绿色文件即可
- sublime text3 控制台 Package Control 的安装与使用方法
下载安装 sublime text3 直接网上搜sublime text3 下载即可,安装很简单 下一步就行,这里主要说明sublime text3 控制台 Package Control 的安装与使 ...
- oracle卸载步骤
一.删除产品 1. 停服务,进入下图,一个个删除Oracle产品,再执行卸载 二.清注册表,开始-搜索- 输入regedit,进入注册表 1.选择HKEY_LOCAL_MACHINE\SOFTWARE ...
- AI机器人最高等级
AI机器人最高等级 题目描述 在小朱朱的游戏世界里,有n个AI机器人. 他们相互之间可以进行PK,胜方存活且升一级,负方直接淘汰. 高等级AI必定战胜低等级AI,同等级AI的PK结果必定一胜一负,且规 ...