Nim or not Nim?

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

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
 

通过SG函数打表可以发现规律。

当n=4*k时,sg[n] = n-1;

当n= 4*k+3 时,sg[n] = n+1;

其余sg[n] = n;

  1. #include <stdio.h>
  2. #include <algorithm>
  3. #include <iostream>
  4. #include <string.h>
  5. using namespace std;
  6. const int MAXN = ;
  7. int SG(int x)
  8. {
  9. if(x == )return x;
  10. if(x % == )return x-;
  11. if(x % == )return x+;
  12. return x;
  13. }
  14. int main()
  15. {
  16. int T;
  17. int n,a;
  18. int sum;
  19. scanf("%d",&T);
  20. while(T--)
  21. {
  22. scanf("%d",&n);
  23. sum = ;
  24. for(int i = ;i < n;i++)
  25. {
  26. scanf("%d",&a);
  27. sum ^= SG(a);
  28. }
  29. if(sum == )printf("Bob\n");
  30. else printf("Alice\n");
  31. }
  32. return ;
  33. }

HDU 3032 Nim or not Nim?(博弈,SG打表找规律)的更多相关文章

  1. hdu 2147 kiki's game(DP(SG)打表找规律)

    题意: n*m的棋盘,一枚硬币右上角,每人每次可将硬币移向三个方向之一(一格单位):左边,下边,左下边. 无法移动硬币的人负. 给出n和m,问,先手胜还是后手胜. 数据范围: n, m (0<n ...

  2. HDU 5795 A Simple Nim(SG打表找规律)

    SG打表找规律 HDU 5795 题目连接 #include<iostream> #include<cstdio> #include<cmath> #include ...

  3. HDU 3032 (SG打表找规律)

    题意: 有n堆石子,alice先取,每次可以选择拿走一堆石子中的1~x(该堆石子总数) ,也可以选择将这堆石子分成任意的两堆.alice与bob轮流取,取走最后一个石子的人胜利. 思路: 因为数的范围 ...

  4. HDU 3032 Nim or not Nim?(SG打表找规律)

    题意: 给你n堆石子,你每次只能操作一堆石子 1.拿去任意个,最少1个 2.把这一堆分成两堆,没有要求对半分 解析+代码: 1 //解题思路: 2 //对于一个给定的有向无环图,定义关于图的每个顶点的 ...

  5. hdu 3032 sg打表找规律 *

    有n堆石子,alice先取,每次可以选择拿走一堆石子中的1~x(该堆石子总数) ,也可以选择将这堆石子分成任意的两堆.alice与bob轮流取,取走最后一个石子的人胜利. 打表代码: #include ...

  6. A Simple Nim (SG打表找规律)

    题意:有n堆石子,每次可以将其中一堆分为数量不为0的3堆,或者从其中一堆中拿走若干个,最终拿完的那个人赢. 思路:直接暴力SG状态,然后找出其中的规律,异或一下每一堆的状态就可以了. #include ...

  7. HDU2149-Good Luck in CET-4 Everybody!(博弈,打表找规律)

    Good Luck in CET-4 Everybody! Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  8. 数学--数论--HDU 1792 A New Change Problem (GCD+打表找规律)

    Problem Description Now given two kinds of coins A and B,which satisfy that GCD(A,B)=1.Here you can ...

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

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

随机推荐

  1. ACM-ICPC 2018 南京赛区网络预赛 Sum

    A square-free integer is an integer which is indivisible by any square number except 11. For example ...

  2. 【Git】GitHub之多人开发一个项目

    首先我们要简单知道github跟Git的区别.git是版本控制工具, github是一个面向开源及私有软件项目的托管平台,也是程序员交流的地方. 接下来就开始讲怎么多人一起开发. 首先我们先拥有git ...

  3. Django【进阶】

    FBV和CBV http://www.cnblogs.com/lucaq/p/7565560.html 中间件 http://www.cnblogs.com/lucaq/p/7581234.html ...

  4. Swift 闭包(六)

    http://blog.csdn.net/huangchentao/article/details/32714185 闭包 Closures 1.闭包表达式 闭包表达式是一种利用简单语法构建内联包的方 ...

  5. 【MT8382/8121】使用绝对路径编译模块会导致recourse_overlay无法应用的问题

    之前为了方便mm模块编译,写了个脚本,实现了在任意模块其子目录下执行脚本即可编译的功能. 其实原理就是一层一层目录地往上寻找Android.mk文件,找到存放Android.mk目录后,就把该目录当作 ...

  6. V-Hyper安装ubuntu-13.10-server-amd64

    1.在windws8上的V_Hyper虚拟机上安装Ubuntu虚拟机服务器版.遇到的问题和解决方案 2.正确的在V-Hyper配置方法参考文章:在Hyper-V中安装和配置Ubuntu Server ...

  7. codevs 线段树练习ⅠⅡⅢ

    1080 线段树练习  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解       题目描述 Description 一行N个方格,开始每个格子里都有 ...

  8. quote(),unquote(),urlencode()编码解码

    quote(),unquote(),quote_plus(),unquote_plus(),urlencode() ,pathname2url(),url2pathname() urllib中还提供了 ...

  9. tcpdump学习(1):安装

    目前学习mysql,其中,提到使用tcpdump来进行query的抓包日志,那么,首先就要安装tcpdump. 在ubuntu中,tcpdump是缺省安装的,如果没有,则按照以下步骤做: 1)安装li ...

  10. xshell连接虚拟机CentOS出现eth0 device not found的解决方法

    昨天用xshell连接虚拟机上的centOS老是连接不上,ifconfig eth0 命令显示 device not found.不知道是什么原因... 折腾了好久 网上是各种搜啊 终于找到解决方法了 ...