博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 3032 Nim or not Nim? (SG函数博弈+打表找规律)
阅读量:5129 次
发布时间:2019-06-13

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

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

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

题意:给定n堆石子,两人轮流操作,每次选一堆石子,取任意石子或则将石子分成两个更小的堆(非0),取得最后一个石子的为胜。

题解:比较裸的SG定理,用sg定理打表,得到表1,2,4,3,5,6,8,7,9,10,12,11...可以发现当x%4==0时sg[x]=x-1;当x%4==3时sg[x]=x+1;其余sg[x]=x。然后异或下就出来结果了。

SG定理打表+找规律:

#include 
#include
#include
#include
using namespace std; const int maxn=1e4+10; int sg[maxn],vis[maxn]; void init() { int i,j,k; sg[0]=0,sg[1]=1; for(i=2;i<=1000;i++) { memset(vis,0,sizeof(vis)); for(j=1;j

AC代码:

#include 
#include
#include
#include
using namespace std; const int maxn=1e6+10; int find(int x) { if(x%4==0)return x-1; else if(x%4==3)return x+1; return x; } int main() { int T; scanf("%d",&T); while(T--) { int a,n,i,j,ans=0; scanf("%d",&n); for(i=0;i

转载于:https://www.cnblogs.com/Ritchie/p/5627242.html

你可能感兴趣的文章
假如女人是一种编程语言,你会更喜欢哪一种
查看>>
debian 64位系统中添加对32位的支持
查看>>
基于CentOS6.5或Ubuntu14.04下Suricata里搭配安装 ELK (elasticsearch, logstash, kibana)(图文详解)...
查看>>
如果遇到Hadoop集群正常,MapReduce作业运行出现错误,如何来查看作业运行日志(图文详解)...
查看>>
硬件驱动程序的知识点滴
查看>>
车牌号码正则表达式
查看>>
Tesseract 4 自行构建支持双引擎的tessdata 文件
查看>>
Find and kill the dead lock process
查看>>
CListCtrl 的应用
查看>>
mongodb
查看>>
合并指定表格指定行的相同文本的相邻单元格
查看>>
双屏幕,鼠标移动设置
查看>>
【BZOJ-3809】Gty的二逼妹子序列 分块 + 莫队算法
查看>>
《Entity Framework 6 Recipes》中文翻译——第十章EntityFramework存储过程处理(四)...
查看>>
收缩自编码器(CAE)
查看>>
AVL平衡树的插入例程
查看>>
Android Studio怎么删除项目
查看>>
shell变量自增 || Python脚本接收参数
查看>>
关于python安装lxml插件的问题
查看>>
Exp7 网络欺诈防范
查看>>