Light OJ 1253 Misere Nim


1253 - Misere Nim
    PDF (English) Statistics Forum
Time Limit: 1 second(s) Memory Limit: 32 MB

Alice and Bob are playing game of Misère Nim. Misère Nim is a game playing on k piles of stones, each pile containing one or more stones. The players alternate turns and in each turn a player can select one of the piles and can remove as many stones from that pile unless the pile is empty. In each turn a player must remove at least one stone from any pile. Alice starts first. The player who removes the last stone loses the game.

Input

Input starts with an integer T (≤ 200), denoting the number of test cases.

Each case starts with a line containing an integer k (1 ≤ k ≤ 100). The next line contains k space separated integers denoting the number of stones in each pile. The number of stones in a pile lies in the range [1, 109].

Output

For each case, print the case number and 'Alice' if Alice wins otherwise print 'Bob'.

Sample Input

Output for Sample Input

3

4

2 3 4 5

5

1 1 2 4 10

1

1

Case 1: Bob

Case 2: Alice

Case 3: Bob



题目大意:
给定有 T组 数据,每组数据有一个数m,然后接下来又m个数,代表的是每一堆有a[i]块石子,然后两个人Alice 和 Bob 玩游戏,Alice先手,每次可以取每一堆的任意石子但是不能不取,谁最后取完谁输。

解题思路:
这就是一个简单的尼姆博弈,但是与前面不一样的是 谁最后取完谁输,以前的是谁最后取完谁赢,这个我们要首先判断一下是不是充裕堆(这个应该在后面的某篇博客中详细介绍),就是判断一下是不是全是1,如果全是的话有几个1,假如是奇数个1的话,先手必输,否则 必赢:如果不全是1的话,那么只需要异或一下,如果异或等于0,后手必赢,否则 必输。

My Code:
<span style="font-size:18px;">#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    int T, m;
    scanf("%d",&T);
    for(int cas=1; cas<=T; cas++)
    {
        scanf("%d",&m);
        int sum = 0, ok = 0;
        for(int i=0; i<m; i++)
        {
            int x;
            scanf("%d",&x);
            if(x != 1)
                ok = 1;
            sum ^= x;
        }
        if(ok)
        {
            if(!sum)
                printf("Case %d: Bob\n",cas);
            else
                printf("Case %d: Alice\n",cas);
        }
        else
        {
            if(m & 1)
                printf("Case %d: Bob\n",cas);
            else
                printf("Case %d: Alice\n",cas);
        }
    }
    return 0;
}</span>


你可能感兴趣的:(Light OJ 1253 Misere Nim)