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. Java开发笔记(九十八)利用Callable启动线程

    前面介绍了如何利用Runnable接口构建线程任务,该方式确实方便了线程代码的复用与共享,然而Runnable不像公共方法那样有返回值,也就无法将线程代码的处理结果传给外部,造成外部既不知晓该线程是否 ...

  2. 关于com工程依赖的一些总结

    作者:朱金灿 来源:http://blog.csdn.net/clever101 一是com组件工程的依赖设置.比如A这个组件工程要使用B组件工程的类,要如何设置呢?具体就是先把在A工程里加上B工程的 ...

  3. AngularJS 整理学习

    参考博客:  https://blog.csdn.net/weixin_33178524/article/details/79179597 https://blog.csdn.net/qq_42128 ...

  4. UINavigationControlle 之 UINavigationBar及navigationItem关系探讨

    在设置标题栏时常常遇到修改标题.修改返回按钮标题.增加一些按钮等需求,实现过程中一般会把UINavigationController.UINavigationBar.navigationItem及se ...

  5. 2018.6.16 PHP小实验

    PHP实验 实验一 <?php /** * Created by PhpStorm. * User: qichunlin * Date: 2018/5/17 * Time: 下午5:35 */ ...

  6. R+hadoop

    这里面的配置 http://www.rdatamining.com/big-data/r-hadoop-setup-guide root@kali:~/hadoop_home/hadoop-1.2.1 ...

  7. AFN post的数据编码格式问题

    想到写任何关于AFN的东西其实我是拒绝的,因为自己这也是第一次用,毕竟AFN现在是最为流行的网络框架了,害怕自己理解的有误,所以不敢造次! 先在这里大致讲解一下过程吧,后期发现了再更正(主要是想让看官 ...

  8. 深入浅出:了解for循环中保留i值得方法

    一.保留i值  通常情况下,因为一些效果我们需要获取到for循环中的i的值,但是往往拿到的都是最后一个i的值.下面介绍几种方法可以获取到i的值 1.自定义属性: arr[i].index = i; 以 ...

  9. Qt之QThread随记

    这是一篇随记,排版什么的就没有那么好了:) 首先要知道,一个线程在资源分配完之后是以某段代码为起点开始执行的,例如STL内的std::thread,POSIX下的pthread等,都是以函数加其参数之 ...

  10. STMS传输队列中的请求状态一直是Running不能结束

    通过STMS传输请求时,遇到了如下问题: STMS传输请求,不论等多久的时间,请求状态一直是running,不能结束.但检查传输的内容时,发现CHANGE REQUEST包含的内容已经传输到目标Cli ...