Nim or not Nim?

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

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
 

题意:Alice和Bob轮流取N堆石子,每堆S[i]个,Alice先,每一次可以从任意一堆中拿走任意个石子,也可以将一堆石子分为两个小堆。先拿完者获胜。

数据范围: (1 ≤ N ≤ 10^6, 1 ≤ S[i] ≤ 2^31 - 1).

思路: 此题为博弈中的—取走-分割游戏(这种游戏允许取走某些东西,然后将原来的一个游戏分成若干个相同的游戏)

由于数据范围,不能直接求sg值只能打表找规律;

有SJ 定理:

对于任意的一个 Anti-SG 游戏,如果我们规定当局面中所有单一游戏的 SG 值为 0 时游戏 结束,则先手必胜当且仅当以下两个条件满足任意一个:

(1)游戏的 SG 函数不为 0,且游戏中某个单一游戏的 SG 函数大于1。

(2)游戏的 SG 函数为 0,且游戏中没有单一游戏的 SG 函数大于 1。

Lasker's Nim游戏:每一轮允许两会中操作之一:①、从一堆石子中取走任意多个,②、将一堆数量不少于2的石子分成都不为空的两堆。

分析:很明显:sg(0) = 0,sg(1) = 1。

状态2的后继有:0,1和(1,1),他们的SG值分别为0,1,0,所以sg(2) =2。

状态3的后继有:0、1、2、(1,2),他们的SG值分别为0、1、2、3,所以sg(3) = 4。

状态4的后继有:0、1、2、3、(1,3)和(2,2),他们的SG值分别为0,1,2,4,5,0,所以sg(4) = 3.

再推一些,推测得到:对于所有的k >= 0,有 sg( 4k+1 ) = 4k+1; sg( 4k+2 ) = 4k+2; sg( 4k+3 ) = 4k+4; sg( 4k+4 ) = 4k+3。

假设游戏初始时有3堆,分别有2、5和7颗石子。三堆的SG函数值分别为2、5、8,他们的Nim和等于15.所以要走到P状态,就要使得第三堆的SG值变成7,可以将第三对按1和6分成两堆。

SG打表代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib> using namespace std; const int N=; int sg[N]; int g(int x){
int mex[];
memset(mex,,sizeof(mex));
if(sg[x]!=-)
return sg[x];
for(int i=x-;i>=;i--)
mex[g(i)]=;
for(int i=;i<=x/;i++){
int ans=;
ans^=g(i);
ans^=g(x-i);
mex[ans]=;
}
for(int i=;;i++)
if(!mex[i])
return sg[x]=i;
} int main(){ freopen("input.txt","r",stdin); int t,n;
scanf("%d",&t);
memset(sg,-,sizeof(sg));
while(t--){
scanf("%d",&n);
int x;
for(int i=;i<n;i++){
scanf("%d",&x);
g(x);
printf("sg[%d]=%d\n",x,sg[x]);
}
for(int i=;i<=;i++){
printf("%d ",sg[i]);
//if(i%10==0)
//system("pause");
}
printf("\n");
}
return ;
}

可得规律:sg(4k)=4k-1;sg(4k+1)=4k+1;sg(4k+2)=4k+2;sg(4k+3)=4k+4;

本题AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib> using namespace std; int main(){ //freopen("input.txt","r",stdin); int t,n;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
int ans=,x;
for(int i=;i<n;i++){
scanf("%d",&x);
if(x%==)
ans^=(x-);
else if(x%== || x%==)
ans^=x;
else
ans^=(x+);
}
if(ans!=)
puts("Alice");
else
puts("Bob");
}
return ;
}

HDU 3032 Nim or not Nim? (sg函数)的更多相关文章

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

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

  2. HDU 1848 Fibonacci again and again(SG函数)

    Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...

  3. hdu 4559 涂色游戏(对SG函数的深入理解,推导打SG表)

    提议分析: 1 <= N <= 4747 很明显应该不会有规律的,打表发现真没有 按题意应该分成两种情况考虑,然后求其异或(SG函数性质) (1)找出单独的一个(一列中只有一个) (2)找 ...

  4. hdu 3980 Paint Chain 组合游戏 SG函数

    题目链接 题意 有一个\(n\)个珠子的环,两人轮流给环上的珠子涂色.规定每次涂色必须涂连续的\(m\)颗珠子,无法继续操作的人输.问先手能否赢. 思路 参考 转化 第一个人取完之后就变成了一条链,现 ...

  5. HDU 1848 Fibonacci again and again SG函数做博弈

    传送门 题意: 有三堆石子,双方轮流从某堆石子中去f个石子,直到不能取,问先手是否必胜,其中f为斐波那契数. 思路: 利用SG函数求解即可. /* * @Author: chenkexing * @D ...

  6. HDU 1524 A Chess Game【SG函数】

    题意:一个N个点的拓扑图,有M个棋子,两个人轮流操作,每次操作可以把一个点的棋子移动到它的一个后继点上(每个点可以放多个棋子),直到不能操作,问先手是否赢. 思路:DFS求每个点的SG值,没有后继的点 ...

  7. Nim游戏与SG函数 ——博弈论小结

    写这篇博客之前,花了许久时间来搞这个SG函数,倒是各路大神的论文看的多,却到底没几个看懂的.还好网上一些大牛博客还是性价比相当高的,多少理解了些,也自己通过做一些题加深了下了解. 既然是博弈,经典的N ...

  8. 博弈论 | 详解搞定组合博弈问题的SG函数

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天这篇是算法与数据结构专题的第27篇文章,我们继续深入博弈论问题.今天我们要介绍博弈论当中非常重要的一个定理和函数,通过它我们可以解决许多 ...

  9. hdu 3032 Nim or not Nim? sg函数 难度:0

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

  10. hdu 3032 Nim or not Nim? (SG函数博弈+打表找规律)

    Nim or not Nim? Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Sub ...

随机推荐

  1. 转:[大数据竞赛]协同过滤在这个问题上是否work

    http://bbs.aliyun.com/read/154433.html?spm=5176.7189909.0.0.gzyohy&fpage=2 看到主办方之前发的一篇文章里提到,这个购买 ...

  2. 单元测试中用@Autowired 报null (空指针异常)

    原因是因为,单元测试不依赖于容器,所以自动注入也就存在问题 (单元测试中加@Autowired注解亲自测过是不行,不知道这样理解的是否正确)

  3. Web - TCP的三次握手

    在TCP/IP协议中,TCP协议提供可靠的连接服务,採用三次握手建立一个连接. 第一次握手:建立连接时,client发送syn包(syn=j)到server,并进入SYN_SENT状态,等待serv ...

  4. Word模板中的表格处理

    在软件系统中,我们经常要输出一些word ,excel,ppt文档,为了输出结果漂亮美观.输出操作方便快捷,通常要制作一些模板文件,通过对模板文件中的关键信息进行修改,就不用管排版.格式等处理了. 在 ...

  5. C/C++中的值传递,引用传递,指针传递,指针引用传递

    在面试过程中,被面试官问到传值和传引用的区别,之前没有关注过这个问题,今天在网上找了一篇包含代码和图片的讲解文章,浅显易懂,遂转载备忘. 1. 值传递 void f( int p){ printf(& ...

  6. C#和网页js互调代码

    C#和网页js互调代码 1.先写个网页放在主程序目录下:test.html <!DOCTYPE html> <html lang="en" xmlns=" ...

  7. POSTGRESQL 自动登录

    以前习惯使用MYSQL命令行登录,但是到POSTGRESQL不能实现,下面总结一下方法: 1.填写需要链接的postgresql语句,一般放在~/.bash_profile,例如: alias log ...

  8. Serializable 介绍

    今天咱们简单介绍一些serializable. 1.序列化是干什么的? 简单说就是为了保存在内存中的各种对象的状态(也就是实例变量,不是方法),并且可以把保存的对象状态再读出来.虽然你可以用你自己的各 ...

  9. angularjs component

    Component https://docs.angularjs.org/guide/component component本质上就是directive. This is a shorthand fo ...

  10. Linux上实现Windows的SQLPlus保存SQL历史记录功能

    在Windows操作系统上,当在DOS命令窗口中运行SQL*Plus的时候,可以使用向上,向下键来跳回之前已经执行过的SQL语句.你可以根据需要修改他们,然后按Enter键重新提交执行. 然而,当在L ...