Nim or not Nim?

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
 

题目大意:

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

(1 ≤ N ≤ 10^6, 1 ≤ S[i] ≤ 2^31 - 1)

解题思路:

对于一个给定的有向无环图。定义关于图的每一个顶点的Sprague-Grundy函数g例如以下:g(x)=mex{ g(y) | y是x的后继 },这里的g(x)即sg[x]

比如:取石子问题,有1堆n个的石子,每次仅仅能取{1。3,4}个石子。先取完石子者胜利。那么各个数的SG值为多少?

sg[0]=0,

n=1时,能够取走{1}个石子,剩余{0}个。mex{sg[0]}={0}。故sg[1]=1;

n=2时,能够取走{1}个石子。剩余{1}个,mex{sg[1]}={1}。故sg[2]=0;

n=3时。能够取走{1,3}个石子,剩余{2,0}个。mex{sg[2],sg[0]}={0,0},故sg[3]=1;

n=4时,能够取走{1,3,4}个石子。剩余{3,1,0}个,mex{sg[3],sg[1],sg[0]}={1,1,0},故sg[4]=2;

n=5时。能够取走{1,3,4}个石子。剩余{4,2,1}个,mex{sg[4],sg[2],sg[1]}={2,0,1},故sg[5]=3;

以此类推.....

     x  0  1  2  3  4  5  6  7  8....

sg[x] 0  1  0  1  2  3  2  0  1....

所以,对于这题:

sg[0]=0

sg[1]=mex{sg[0] }=1

sg[2]=mex{sg[0],sg[1],sg[1,1] }=mex{0,1,1^1}=2;

sg[3]=mex{sg[0],sg[1],sg[2],sg[1,2]}=mex{0,1,2,1^2}=mex{0,1,2,3}=4;

sg[4]=mex{sg[0],sg[1],sg[2],sg[3],sg[1,3],sg[2,2]}=mex{0,1,2,4,5,0}=3;

..............................................................................

能够发现:sg[4*k+1]=4*k+1,sg[4*k+2]=4*k+2,  sg[4*k+3]=4*k+4,sg[4*k+4]=4*k+3

解题代码:

#include <iostream>
using namespace std; int main(){
int t;
cin>>t;
while(t-- >0){
int sg=0,n,x;
cin>>n;
for(int i=0;i<n;i++){
cin>>x;
if(x%4==0) sg^=x-1;
else if(x%4==3) sg^=x+1;
else sg^=x;
}
if(sg) cout<<"Alice"<<endl;
else cout<<"Bob"<<endl;
}
return 0;
}

版权声明:本文博主原创文章。博客,未经同意不得转载。

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  8. NIM游戏,NIM游戏变形,威佐夫博弈以及巴什博奕总结

    NIM游戏,NIM游戏变形,威佐夫博弈以及巴什博奕总结 经典NIM游戏: 一共有N堆石子,编号1..n,第i堆中有个a[i]个石子. 每一次操作Alice和Bob可以从任意一堆石子中取出任意数量的石子 ...

  9. 博弈论初步(SG函数)

    讲解见此博客https://blog.csdn.net/strangedbly/article/details/51137432 理解Nim博弈,基于Nim博弈理解SG函数的含义和作用. 学习求解SG ...

随机推荐

  1. Java基础之参数传递

    public class ArgsTransfer { /* * 基本数据类型直接存储在变量中,函数参数传递时,是将变量中存储的数据拷贝,函数中改变形参,和调用处的实参是不同的变量,两边互不影响 * ...

  2. 在CentOS 7 / Gnome 3 双屏时设置主屏

    在Windows中设置扩展显示器为主屏的方式非常清楚,但在Linux中就不是那么明显了,下面介绍如何完成这个设置 ------------------------------------------- ...

  3. 找出n个数中出现了奇数次的两个数

    如果是找只出现了奇数次的一个数, 那么我们从头异或一遍就可以. 那么如何找出现了奇数次的两个数呢? 首先我们还是从头异或一遍, 然后结果肯定不为0, 对于异或出来的结果, 如果这个数的某一位是1, 说 ...

  4. Redhat Linux内核升级全记录(转)

        http://www.sina.com.cn 2001/06/15 15:38 中国电脑教育报 李红   Redhat Linux因为比较容易上手,所以用户很多.它系统配置完善,预装了丰富的应 ...

  5. 转: 关于异步promises

    迄今为止,可能每个JavaScript开发者和他们的祖母都听说过Promises.如果你没有,那么你即将会.promises的概念是由CommonJS小组的成员在 Promises/A规范 中提出来的 ...

  6. Thread-safety with regular expressions in Java

    As mentioned in our introduction to the Pattern and Matcher classes, the Java regular expression API ...

  7. perl lwp get uft-8和gbk

    gbk编码: jrhmpt01:/root/lwp# cat x2.pl use LWP::UserAgent; use DBI; $user="root"; $passwd='R ...

  8. Web平台开发流程以及规范

    1.js和css的放的位置顺序与加载速度分析 为了让客户先看到效果,必须要先加CSS 如果在先head加载CSS,如果CSS大,会先下载CSS,再渲染HTML标签 如果CSS放在head中,当显示出H ...

  9. protobuf 中的嵌套消息的使用

    protobuf的简单的使用,不过还留下了一个问题,那就是之前主要介绍的都是对简单数据的赋值,简单数据直接采用set_xx()即可,但是如果不是简单变量而是自定义的复合类型变量,就没有简单的set函数 ...

  10. Cookie获取、设置值

    设置: HttpCookie cookie = new HttpCookie("cookieName"); cookie.Value = "name1" Htt ...