博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 3032 Nim or not Nim?(Multi_SG,打表找规律)
阅读量:4991 次
发布时间:2019-06-12

本文共 3257 字,大约阅读时间需要 10 分钟。

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

 

打表代码

1 #include
2 #include
3 #include
4 5 using namespace std; 6 7 int sg[1010]; 8 bool Hash[1010]; 9 const int N = 1000;10 11 void get_SG() {12 for (int i=1; i<=N; ++i) {13 memset(Hash,false,sizeof(Hash));14 for (int j=0; j<=i; ++j) {15 Hash[sg[j]] = true; // 取石子16 if (j!=0 && j!=N) Hash[sg[j]^sg[i-j]] = true; //拆分17 }18 for (int j=0; ; ++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=1; i<=100; ++i) {32 printf("%d ",sg[i]);33 if (i % 4 == 0) puts("");34 }35 return 0;36 }
View Code

 

转载于:https://www.cnblogs.com/mjtcn/p/8481846.html

你可能感兴趣的文章
Android属性动画
查看>>
第九周作业
查看>>
hihocoder 1523 数组重排2+思维
查看>>
004 面向对象1
查看>>
C语言——贪吃蛇(第二阶段小蛇的移动
查看>>
牛客网——二叉树
查看>>
MyEclipse反编译插件的安装
查看>>
php RSA 简单实现
查看>>
python_Day4
查看>>
mongo3.0用户设置转(3)
查看>>
2018.3 强网杯 部分writeup
查看>>
架构师速成6.18-初中书单资料推荐
查看>>
linux系统的安装
查看>>
Java设计模式菜鸟系列(十三)建模和实现状态模式
查看>>
《Hadoop》对于高级编程Hadoop实现构建企业级安全解决方案
查看>>
android ndk通过遍历和删除文件
查看>>
Notification(一个)——使用演示样本的基础知识
查看>>
《算法导论》为什么经典
查看>>
windows如何能在“运行”框输入名称就启动相应的软件
查看>>
修复反编译资源文件及批量修复程序源码
查看>>