HDU 3032Nim or not Nim Nim博弈 规律
来源:互联网 发布:投资失败一无所有 知乎 编辑:程序博客网 时间:2024/05/23 15:36
Nim or not Nim?
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64uAppoint description:
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.
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
232 2 323 3
Sample Output
AliceBob
如果不是数学大牛,基本上不太可能通过理论分析来求解这道题目,目前我们只知道求解这道题目需要计算每一堆物品的SG值,但这又不是传统的Nim博弈,加上数据范围非常大,那看来我们只有通过规律来求解了,这也是许多博弈问题的解法,幸运的是,一般这种问题的结论都会非常简单。
既然要找规律,那我们就要先手工算出一些SG值,看能否从中看出一些规律,这就考验大家的基础知识了。
就这道题目而言,由于每一堆物品的处理方式是相同的,因此我们可以只考虑一堆物品。
g(0)=0,g(1)=1;这是很显然的
g(2)=2;因为其后继状态为0,1;分解为(1,1)。g(2)=mex(0,1)=2
g(3)=4;其后继为0,1,2,;分解为(1,2)。g(3)=mex(0,1,2,3)=4
g(4)=3;后继为0,1,2,3;分解为(1,3),(2,2),g(4)=mex(0,1,2,4,5)=3
g(5)=5;
g(6)=6;
g(7)=8;
......
也许你已经看出了什么,也许你还是一头雾水,我首先说一下结论
if(n%4==3)
return n+1;
else if(n%4==0)
return n-1;
else
return n;
如果实在看不出来,还可以打一个表,数据量更多的情况下会更容易观察
#include <iostream>#include <cmath>#include <stdio.h>#include <string>#include <cstring>#include <map>#include <set>#include <vector>#include <stack>#include <queue>#include <iomanip>#include <algorithm>using namespace std;int get_sg(int n){ if(n%4==3) return n+1; else if(n%4==0) return n-1; else return n;}int main(){ int T,N,heap; int ans; scanf("%d",&T); while(T--) { scanf("%d",&N); ans=0; while(N--) { scanf("%d",&heap); ans^=get_sg(heap); } printf("%s\n",ans==0?"Bob":"Alice"); } return 0;}
0 0
- HDU 3032Nim or not Nim Nim博弈 规律
- hdu 3032 Nim or not Nim? 博弈
- HDU 2311-Nim or not Nim?(Nim博弈-打sg表找规律)
- hdu 3032 Nim or not Nim? 博弈(SG定理+找规律)
- HDU 3032 Nim or not Nim?(博弈 SG打表找规律)
- hdu 3032 Nim or not Nim? (SG打表找规律)
- HDU 3032 Nim or not Nim? SG打表找规律
- hdu 3032 Nim or not Nim? (sg函数规律)
- HDU 3032 Nim or not Nim?(sg函数博弈)
- HDU 3032 Nim or not Nim?(博弈 sg表)
- (HDU 3032) Nim or not Nim(SG函数、博弈)
- HDU 3032 Nim or not Nim?
- Nim or not Nim? - HDU 3032 博弈论
- HDU 3032Nim or not Nim?
- 【hdu 3032】Nim or not Nim?
- hdu 3032 Nim or not Nim?
- Nim or not Nim? HDU 3032
- HDU 3032 Nim or not Nim?
- 单调递增最长子序列
- Vega Prime 新手 入门指引
- 关于MySQL数据库重装失败,出现问题,重装的时候出现错误1130的问题和解决办法。
- 第四次c++作业——输出星号图
- UFIDL稀疏自编码代码实现及解释
- HDU 3032Nim or not Nim Nim博弈 规律
- 【GDOI2016模拟4.23】轻重路径
- Mysql 语句执行顺序
- python 文件编码转换 从vs下的默认编码到utf-8
- Android源码桥梁模式---FragmentCompat
- 1002 写出这个数 (20)
- Android读书笔记(一)Activity退出时释放内存
- qsort和sort的一些小结
- 4月份英语学习