Nim or not Nim?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3759    Accepted Submission(s): 1937

Problem Description
Nim is a two-player mathematic game of strategy in which players take turns removing objects from distinct heaps. On each turn, a player must remove at least one object, and may remove any number of objects provided they all come from the same heap.

Nim is usually played as a misere game, in which the player to take the last object loses. Nim can also be played as a normal play game, which means that the person who makes the last move (i.e., who takes the last object) wins. This is called normal play because most games follow this convention, even though Nim usually does not.

Alice and Bob is tired of playing Nim under the standard rule, so they make a difference by also allowing the player to separate one of the heaps into two smaller ones. That is, each turn the player may either remove any number of objects from a heap or separate a heap into two smaller ones, and the one who takes the last object wins.

 
Input
Input contains multiple test cases. The first line is an integer 1 ≤ T ≤ 100, the number of test cases. Each case begins with an integer N, indicating the number of the heaps, the next line contains N integers s[0], s[1], ...., s[N-1], representing heaps with s[0], s[1], ..., s[N-1] objects respectively.(1 ≤ N ≤ 10^6, 1 ≤ S[i] ≤ 2^31 - 1)
 
Output
For each test case, output a line which contains either "Alice" or "Bob", which is the winner of this game. Alice will play first. You may asume they never make mistakes.
 
Sample Input
2
3
2 2 3
2
3 3
 
Sample Output
Alice
Bob
 
Source
 
Recommend
gaojie   |   We have carefully selected several similar problems for you:  3031 3033 3038 3035 3034 
 

题解:

每一轮允许两会中操作之一:①从一堆石子中取走任意多个②将一堆数量不少于2的石子分成都不为空的两堆。
这个问题可以用SG函数来解决。首先,操作①其实和Nim游戏没什么区别,对于一个石子数为k的点来说,后继可以为0…k-1。而操作②实际上是把一个游戏分成了两个游戏。根据游戏的和的概念,这两个游戏的和应该为两个子游戏的SG函数值的异或。而求某一个点的SG函数要利用它的后继,它的后继就应该为当前局面能产生的所有单一游戏,以及当前局面所有能分成的多个单一游戏的游戏的和。
比如说,状态3的后继有:0、1、2、(1,2),他们的SG值分别为0、1、2、3,所以sg(3) = 4。
那么这样,我们就能用SG函数解决这个问题。
但是,如果数据范围非常大的时候SG函数就不能用了,通过打表可以发现一个更优美的结论:

if(x%4==0) sg[x]=x-1;
if(x%4==1||x%4==2) sg[x]=x;
if(x%4==3) sg[x] = x+1。

 
参考代码:
#include<bits/stdc++.h>
using namespace std;
#define N 1000000
int T,n,x,ans; int get_sg(int x)
{
if(x%==) return x-;
else if(x%==||x%==) return x;
else if(x%==) return x+;
}
int main()
{
scanf("%d",&T);
while (T--)
{
scanf("%d",&n);ans=;
for(int i=;i<=n;++i)
{
scanf("%d",&x);
ans^=get_sg(x);
}
if(ans) puts("Alice");
else puts("Bob");
}
return ;
}

HDU3032 Nim or not Nim?(Lasker’s Nim游戏)的更多相关文章

  1. HDU.3032.Nim or not Nim?(博弈论 Lasker's Nim)

    题目链接 \(Description\) 有多堆石子, 每次可以将任意一堆拿走任意个或者将这一堆分成非空的两堆, 拿走最后一颗石子的人胜利.问谁会获得胜利. \(Solution\) Lasker's ...

  2. [ACM_数学] Fibonacci Nim(另类取石子,2-4组合游戏)

    游戏规则: 有一堆个数为n的石子,游戏双方轮流取石子,满足: 1)先手不能在第一次把所有的石子取完: 2)之后每次可以取的石子数介于1到对手刚取的石子数的2倍之间(包含1和对手刚取的石子数的2倍). ...

  3. hdu 3032 Nim or not Nim? 博弈论

     这题是Lasker’s Nim. Clearly the Sprague-Grundy function for the one-pile game satisfies g(0) = 0 and g( ...

  4. 博弈论之Nim

    博弈论(一):Nim游戏 重点结论:对于一个Nim游戏的局面(a1,a2,...,an),它是P-position当且仅当a1^a2^...^an=0,其中^表示位异或(xor)运算. Nim游戏是博 ...

  5. HDU 3032 Nim or not Nim? (sg函数)

    Nim or not Nim? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  6. 【HDU1730】Northcott Game(Nim问题)

    Northcott Game Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  7. 创建nim+安装vioc分区

    一.搭建nim服务器 1.安装须知 安装nim软件包,client随系统默认安装,这里需要安装的是bos.sysmgt.nim.master和bos.sysmgt.nim.spot,即bos.sysm ...

  8. 编程之美----NIM游戏

    : 博弈游戏·Nim游戏 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 今天我们要认识一对新朋友,Alice与Bob.Alice与Bob总是在进行各种各样的比试,今天他 ...

  9. Nim教程【一】

    这应该是国内第一个关于Nim入门的系列教程 什么是Nim 我们先来引述网友 Luikore的一段话: Nim 不是函数式的, 但 Nim 支持卫生宏, 可以做 AST 重写, 可以自定编译规则, 是静 ...

随机推荐

  1. AI的真实感

    目录 1.让AI"不完美"--估算和假设 2 AI感知 全能感知 特定感觉无知 3 AI的个性 4 AI的预判 5 AI的智能等级 ​ AI的真实感一直是游戏AI程序员追求的目标, ...

  2. activemq 的延迟队列和幂等性检查

    一. 延迟消息队列 1. 在提交支付之后,可以发送一个延迟检查的队列,来主动查询用户在支付宝上的支付状态 在mq的配置/config/activeMq.xml的broker实例上配置 schedule ...

  3. PHP数组与xml互相转换

    1.数组转xml function arrayToXml($arr) { $xml = "<xml>"; foreach ($arr as $key => $va ...

  4. vue 实现单选/多选效果

    转:https://blog.csdn.net/Number7421/article/details/81002729 不过我以前都写过这三种方法了,很pang额,怕之后忘记了,偷个懒拿别人的,以免以 ...

  5. 在idea中使用git

    在idea中使用git 1. 在idea中配置git ​ 安装好IntelliJ IDEA后,如果Git安装在默认路径下,那么idea会自动找到git的位置,如果更改了Git的安装位置则需要手动配置下 ...

  6. 线程池ThreadPoolExecutor的使用方法

    方法我们通过继承Thread类和实现runnable接口或者callable接口三种方式实现. 继承Thread类实际上也是实现了runnable接口,被继承的类主要是实现run()方法,通过star ...

  7. linux网络配置(ifcfg)

    将linux主机接入到网络需要配置哪些配置项? IP/NETMASK:本地通信. 路由(网管):跨网络通信. DNS服务器地址:基于主机名通信. DNS服务器有三种:主/备用DNS服务器/第三备份dn ...

  8. sublimetext使用教程

    图片来自网络,仅供参考 前言 随着我们编写的代码越来越复杂,DevC++以不再能满足我们的需求,所以,我们需要 一个能够进行调试,编译,运行等等功能的现代化ide,sublimetext(以下简称ST ...

  9. Redis的面试问题总结,面试跳槽必备

    1.什么是redis? Redis 是一个基于内存的高性能key-value数据库. 2.Reids的特点 Redis本质上是一个Key-Value类型的内存数据库,很像memcached,整个数据库 ...

  10. Java通过 Scanner 类来获取用户的输入

    通过 Scanner 类来获取用户的输入. import java.util.Scanner; Scanner s = new Scanner(System.in);// 从键盘接收数据  Syste ...