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 & %I64u
Submit Status Practice HDU 3032
Appoint 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.
 

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