Nim or not Nim?

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

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
 
 

分析

打表找规律。

$$
sg(x)=
\begin{cases}
x-1,&x\ mod\ 4=0\\
x+1,&x\ mod\ 4=3\\
x,&else \\
\end{cases}
$$

code

  1. #include<cstdio>
  2. #include<algorithm>
  3.  
  4. using namespace std;
  5.  
  6. int get_SG(int x) {
  7. if (x%==) return x-;
  8. else if (x%==) return x+;
  9. return x;
  10. }
  11. int main () {
  12. int T,n,ans;
  13. scanf("%d",&T);
  14. for (int Case=; Case<=T; ++Case) {
  15. scanf("%d",&n);
  16. ans = ;
  17. for (int t,i=; i<=n; ++i) {
  18. scanf("%d",&t);
  19. ans ^= get_SG(t);
  20. }
  21. if (ans==) puts("Bob");
  22. else puts("Alice");
  23.  
  24. }
  25. return ;
  26. }

打表代码

  1. #include<cstdio>
  2. #include<algorithm>
  3. #include<cstring>
  4.  
  5. using namespace std;
  6.  
  7. int sg[];
  8. bool Hash[];
  9. const int N = ;
  10.  
  11. void get_SG() {
  12. for (int i=; i<=N; ++i) {
  13. memset(Hash,false,sizeof(Hash));
  14. for (int j=; j<=i; ++j) {
  15. Hash[sg[j]] = true; // 取石子
  16. if (j!= && j!=N) Hash[sg[j]^sg[i-j]] = true; //拆分
  17. }
  18. for (int j=; ; ++j)
  19. if (!Hash[j]) {sg[i] = j;break;};
  20. }
  21. }
  22.  
  23. int main () {
  24. get_SG();
  25. /*for (int i=1; i<=10; ++i) {
  26. for (int j=1; j<=10; ++j) {
  27. printf("%d ",sg[(i-1)*10+j]);
  28. }
  29. puts("");
  30. }*/
  31. for (int i=; i<=; ++i) {
  32. printf("%d ",sg[i]);
  33. if (i % == ) puts("");
  34. }
  35. return ;
  36. }

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

  1. HDU 5753 Permutation Bo (推导 or 打表找规律)

    Permutation Bo 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5753 Description There are two sequen ...

  2. HDU 3032 Nim or not Nim?(博弈,SG打表找规律)

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

  3. HDU 4919 Exclusive or (数论 or 打表找规律)

    Exclusive or 题目链接: http://acm.hust.edu.cn/vjudge/contest/121336#problem/J Description Given n, find ...

  4. HDU 4861 Couple doubi (数论 or 打表找规律)

    Couple doubi 题目链接: http://acm.hust.edu.cn/vjudge/contest/121334#problem/D Description DouBiXp has a ...

  5. HDU 4588 Count The Carries 数位DP || 打表找规律

    2013年南京邀请赛的铜牌题...做的非常是伤心.另外有两个不太好想到的地方.. ..a 能够等于零,另外a到b的累加和比較大.大约在2^70左右. 首先说一下解题思路. 首先统计出每一位的1的个数, ...

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

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

  7. HDU 5795 A Simple Nim (博弈 打表找规律)

    A Simple Nim 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5795 Description Two players take turns ...

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

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

  9. hdu_5795_A Simple Nim(打表找规律的博弈)

    题目链接:hdu_5795_A Simple Nim 题意: 有N堆石子,你可以取每堆的1-m个,也可以将这堆石子分成3堆,问你先手输还是赢 题解: 打表找规律可得: sg[0]=0 当x=8k+7时 ...

随机推荐

  1. Linux下安装软件遇见的问题汇总

    1.安装monodevelop 安装环境Linux Mint17.1 在软件在中心直接安装monodevelop,安装完成后直接启动界面“一闪而过”,解决办法: 软件中心安装 mono-complet ...

  2. Java中的do-while循环——通过示例学习Java编程(11)

    作者:CHAITANYA SINGH 来源:https://www.koofun.com/pro/kfpostsdetail?kfpostsid=22&cid=0 在上一篇教程中,我们讨论了w ...

  3. 零基础逆向工程28_Win32_02_事件_消息_消息处理函数

    1 第一个图形界面程序 步骤1:创建Windows应用程序 选择空项目 步骤2:在新建项窗口中选C++代码文件 创建一个新的cpp文件 步骤3:在新的cpp文件中添加:#include <Win ...

  4. centos7.4 安装后的基本设置

    centos7.4 安装后的基本设置 设置主机名称 设置IP地址,网关 修改网卡名称 内核优化 系统安全设置 防火墙设置 ssh设置 同步系统时间 安装基础软件包 软件配置 设置主机名称 hostna ...

  5. Oracle添加自增长字段方法步骤

    第一步:创建自增长序列 CREATE SEQUENCE ZH_ALARM_INFO_SEQ--自动增长列 INCREMENT BY 1 -- 每次加几个 START WITH 1 -- 从1开始计数 ...

  6. springMvc-reset风格和对静态资源的管理

    1.所谓rest风格及比较优雅的,没有一大堆后缀的风格 2.对静态资源的管理,及样式.图片等不需要springMvc过滤 代码: 1.在springMvc的配置文件中添加mvc标签 <?xml ...

  7. 【文件拷贝】使用Total Commander Portable拖动拷贝文件,支持队列

    使用Total Commander Portable,可以批量拷贝多个位于相同或不同目录的文件(夹)到指定的相同或不同的目录.这样避免了同时复制多个大的文件造成的速度减慢:将所有任务手动操作之后,剩下 ...

  8. Python http

    # import httplib # http_client = None # http_client = httplib.HTTPConnection('localhost', 8080, time ...

  9. ShopNC B2B2C多用户商城网站系统源码

    直接正常安装就行哦 注意有服务器的安装可以更下安装时间的长度 也就是说进行跳转的 如果时间太少 这样会安装不成 数据导入不完成 所以就会安装不成功安装好后打开data\config\config.in ...

  10. [转载]AngularJS入门教程04:双向绑定

    在这一步你会增加一个让用户控制手机列表显示顺序的特性.动态排序可以这样实现,添加一个新的模型属性,把它和迭代器集成起来,然后让数据绑定完成剩下的事情. 请重置工作目录: git checkout -f ...