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. 关于Google指令(别提baidu)

    关于google指令 关于google指令 google为我们准备好了的"指令"(directive),可以最大限度帮助我们完成每一次搜索.这些指令其实就是一个个关键字,能让我们从 ...

  2. javascript小练习—点击将DIV变成红色(通过for循环遍历)

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  3. php中的require-once

    require_once语句和require语句完全相同,唯一区别是 PHP 会检查该文件是否已经被包含过,如果是则不会再次包含. 参见include_once的文档来理解_once的含义,并理解与没 ...

  4. js跨域及解决方法

    什么是跨域 JavaScript出于安全方面的考虑,不允许跨域调用其他页面的对象.但在安全限制的同时也给注入iframe或是ajax应用上带来了不少麻烦.这里把涉及到跨域的一些问题简单地整理一下: 首 ...

  5. Linux中的盘符问题

    在windows 中像 C.D.E.F这些都可以当盘符,就是说对应了我们所看到的C盘,D盘,E盘,F盘.然而是不是只能加26个硬盘了呢? 盘符到硬盘也只是一个对映关系,我们也是可以建立从一个文件夹到一 ...

  6. TMS X-Cloud Todolist with FNC

    Wednesday, June 22, 2016 It's almost three months since we released the first version of the TMS FNC ...

  7. HTTP使用BASIC认证的原理及实现方法(还有NTLM方法,比较复杂)

    一.   BASIC认证概述 在HTTP协议进行通信的过程中,HTTP协议定义了基本认证过程以允许HTTP服务器对WEB浏览器进行用户身份证的方法,当一个客户端向HTTP服务 器进行数据请求时,如果客 ...

  8. qt4.8.4安装以及64位程序编译方法

    本文将使用简单的几个步骤说明在vc2008和64位的操作系统下如何编译安装x64Qt软件 首先必须保证你所使用的系统是64bit的操作系统,本次我们使用的系统是windows7 professiona ...

  9. 如何得到Sessionid的值

    当用户向一个网站请求第一个页面时,用户会话启动.当第一个页面被请求时,web服务器将asp.net_sessionID  cookie添加进用户的浏览器.可以使用newsession属性探测新会话的启 ...

  10. C#获取时间的函数

    //获取日期+时间DateTime.Now.ToString();            // 2012-9-4 20:02:10DateTime.Now.ToLocalTime().ToString ...