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. 能够在Linux系统中运行的5款大型耐玩游戏

    Linux 可能不会很快成为游戏玩家选择的平台 —— Valve Steam Machines 的失败似乎是对这一点的深刻提醒 —— 但这并不意味着该平台没有稳定增长,并且拥有相当多的优秀游戏. 从独 ...

  2. 为什么有的需要安全连接的的application只有开Fiddler才好用?

      Help! Running Fiddler Fixes My App??? Over the years, the most interesting class of support reques ...

  3. CareerCup Facebook Total number of substring palindrome

    Write a function for retrieving the total number of substring palindromes.  For example the input is ...

  4. 微服务架构实践 - 你只懂docker与spring boot就够了吗?

    微服务架构实践 - 你只懂docker与spring boot就够了吗? 作者 浮云发发 已关注 2017.02.27 02:50* 字数 2613 阅读 2583评论 6喜欢 35赞赏 2 微服务并 ...

  5. PPT模板中的”书签”

    引言 在项目中生成文档报告经常需要word中,其中的关键就是书签,通过定位和替换书签中的值来达到生成定制的报告(详见Word模板中的表格处理):但在PPT中却没有书签这个概念,所以,不能采用这种方式. ...

  6. 面试小结之Elasticsearch篇

    https://www.cnblogs.com/luckcs/articles/7052932.html

  7. VS2015 之 常用快捷键

    调试执行 F5,终止调试执行 Shift+F5 启动执行 Ctrl+F5 查找下一个 F3,查找上一个 Shift+F3 附加到进程 Ctrl+Alt+P,逐过程 F10,逐语句执行 F11 切换断点 ...

  8. MYSQL判断不存在时创建表或创建数据库

    创建数据库: Create Database If Not Exists MyDB Character Set UTF8 创建数据表: Create Table If Not Exists `worl ...

  9. WebSocket【转】

    1.什么是WebSocket WebSocket 是一种自然的全双工.双向.单套接字连接.使用WebSocket,你的HTTP 请求变成打开WebSocket 连接(WebSocket 或者WebSo ...

  10. vue循环中的v-show

    v-show如果使用循环对象的属性来时控制, 这个属性必须是加载时就存在的 <div class="list-group col-sm-12" v-for="(is ...