您的位置:首页 > 其它

hdoj3032 Nim or not Nim?( 可分解尼姆 )

2014-03-19 18:24 239 查看



Nim or not Nim?

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 810 Accepted Submission(s): 385



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




题意:规则和nim博弈取n堆石子一样,n堆石子,游戏双方每次可以取某一堆的任意个,不可以不取,还可以把一堆分成两堆,最后没办法操作的一方为输家!

分析:可以看出比nim游戏加了一个条件,可以分解为两堆。

对于某一堆来说,其实可以用sg函数求解。

但是题目数据位2^31次方,因此肯定是找规律题目。

对于一堆石子,用sg函数的话。

sg【0】=0 sg【1】=1

石子数为 2 ,每次可取 0 ,1,个,还可以分解为(1,1)(0,2),结果都为0(分解为相同的和有0的结果都为0,不影响结果,故以后不写),根据nim规律

mex(2)=( 0 , 1 ) = 2;

石子数为 3 ,每次可取 0 , 1 , 2 个,还可以分解成 (1,2),结果为sg [ 1 ] ^ s g [ 2 ] = 3,mex ( 3 ) = ( 0 , 1 ,2 ,3 ) = 4;

石子数为 4 ,每次可取 0 ,1 , 2 ,3(sg【3】=4) 个,还可以分解为 (1,3),结果为sg [ 1 ] ^ sg [ 3 ] = 2,mex( 4 ) = ( 0,1,2,4 ) = 3;

依次推理发现: sg【5】= 5,sg【6】=6,sg【7】=8,sg【7】 。

会发现 当 n%4==3时 sg【n】= n+1, n%4==0时,sg【n】 = n-1,其他是本身、

可以写个程序计算一下。(发现上面规律)

#include<iostream>
#include<cstdio>
#include<cstring>

const int N=10010;

int sg
;

int g(int x){
    int mex[1010];
    memset(mex,0,sizeof(mex));
    if(sg[x]!=-1)
        return sg[x];
    for(int i=x-1;i>=0;i--)
        mex[g(i)]=1;
    for(int i=1;i<=x/2;i++){
        int ans=0;
        ans^=g(i);
        ans^=g(x-i);
        mex[ans]=1;
    }
    for(int i=0;;i++)
        if(!mex[i])
            return sg[x]=i;
}

int main(){
    int t,n;
    scanf("%d",&n);
    memset(sg,-1,sizeof(sg));
    for(int i=0;i<n;i++){
        sg[i]=g(i);
        printf("sg[%d]=%d\n",i,sg[i]);
    }
}


那么就可以用 nim 游戏的规律求解了。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>

using namespace std;

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n,x,ans=0;
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            scanf("%d",&x);
            if(x%4==3)
                ans^=(x+1);
            else if(x%4==0)
                ans^=(x-1);
            else
                ans^=x;
        }
        if(ans==0)
            printf("Bob\n");
        else
            printf("Alice\n");
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: