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

 #include<cstdio>
#include<algorithm> using namespace std; int get_SG(int x) {
if (x%==) return x-;
else if (x%==) return x+;
return x;
}
int main () {
int T,n,ans;
scanf("%d",&T);
for (int Case=; Case<=T; ++Case) {
scanf("%d",&n);
ans = ;
for (int t,i=; i<=n; ++i) {
scanf("%d",&t);
ans ^= get_SG(t);
}
if (ans==) puts("Bob");
else puts("Alice"); }
return ;
}

打表代码

 #include<cstdio>
#include<algorithm>
#include<cstring> using namespace std; int sg[];
bool Hash[];
const int N = ; void get_SG() {
for (int i=; i<=N; ++i) {
memset(Hash,false,sizeof(Hash));
for (int j=; j<=i; ++j) {
Hash[sg[j]] = true; // 取石子
if (j!= && j!=N) Hash[sg[j]^sg[i-j]] = true; //拆分
}
for (int j=; ; ++j)
if (!Hash[j]) {sg[i] = j;break;};
}
} int main () {
get_SG();
/*for (int i=1; i<=10; ++i) {
for (int j=1; j<=10; ++j) {
printf("%d ",sg[(i-1)*10+j]);
}
puts("");
}*/
for (int i=; i<=; ++i) {
printf("%d ",sg[i]);
if (i % == ) puts("");
}
return ;
}

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. 认识与入门 MarkDown (转Te_Lee)

    Markdown 是一种轻量级的「标记语言」,它的优点很多,目前也被越来越多的写作爱好者,撰稿者广泛使用.看到这里请不要被「标记」.「语言」所迷惑,Markdown 的语法十分简单.常用的标记符号也不 ...

  2. Json Web Token 简介

    1.Json Web Token是干什么        简称JWT,在HTTP通信过程中,进行身份认证.       我们知道HTTP通信是无状态的,因此客户端的请求到了服务端处理完之后是无法返回给原 ...

  3. Bootstrap插件-collapse

    <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title> ...

  4. shell实现网站备份

    #!/bin/bash ##back web directory scripts #需要备份的目录写入与脚本同级目录test.txt文件中 DIR="/data/server/www&quo ...

  5. 关于wp8.1 runtime模式下面的摄像头调用拍照问题和应用生命周期问题

    现在的msdn文档,还找不到详细的wp8.1的摄像头拍照文档,只有一个序列拍照,类似九连拍的文档,而且这文档感觉就是windows8.1搬过来应付的,wp8.1模式,只要有一个地方处理不好,手机就会死 ...

  6. C#中?和??用法

       在C#中“?”有三种用法.       1.可空类型修饰符(?):引用类型可以使用空引用表示一个不存在的值,而值类型通常不能表示为空,例如:string str=null;是正确的.int i= ...

  7. IOS tableView的数据刷新

    1.tableView的刷新 1> 数据刷新的总体步骤 * 修改模型数据 * 刷新表格(刷新界面) 2> 刷新表格(刷新界面)的方法 * 全局刷新(每一行都会重新刷新) - (void)r ...

  8. C++中的异常安全性

    http://blog.csdn.net/bonchoix/article/details/8046727 一个函数如果说是“异常安全”的,必须同时满足以下两个条件:1.不泄漏任何资源:2.不允许破坏 ...

  9. 20.JSON

    JSON是javascript的一个子集,利用js中的一些儿模式来表示结构化数据.不是只有javascript才使用JSON,JSON是一种数据格式,很多编程语言都有针对JSON的解析器和序列化器. ...

  10. python_66_生成器2

    import time def consumer(name): print('%s准备吃包子 '%name) while True: baozi=yield print('包子[%s]来了,被[%s] ...