Misere Nim

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

3

4

2 3 4 5

5

1 1 2 4 10

1

1

Sample Output

Case 1: Bob

Case 2: Alice

Case 3: Bob

思路:这道题就是典型的nim游戏博弈题。如果条件满足a1^a2^...^an = 0,则a赢(先取完最后一个石子的人输)。反之则b赢。

          特判:如果每堆石子都只有一个石头,那奇数堆的时候b赢,否则a赢。

详细解释请看上一篇博客(傻笑.jpg)

#include <cstdio>
#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cmath>
#include <stack>>
using namespace std;
#define ll long long
const int inf = 0x3f3f3f3f;
const  ll linf  =1LL<<50;
const int maxn = 1e5+8;
int t, n, x, sum;
int main()
{
    scanf("%d", &t);
    int miao = t;
    while(t--)
    {
        sum = 0;
        bool flag = 1;
        scanf("%d", &n);
        for(int i = 0; i<n; i++)
        {
            scanf("%d", &x);
            if(x != 1)flag =0;
            sum ^= x;
        }
        if(flag)//如果所有的石碓都是只有一个石子
        {
            if(n&1)printf("Case %d: Bob\n", miao-t);//如果堆数为奇数,则b赢
            else printf("Case %d: Alice\n", miao-t);
        }
        else
        {
            if(!sum) printf("Case %d: Bob\n", miao-t);//如果这个状态为非平衡状态,则b赢
            else printf("Case %d: Alice\n", miao-t);
        }
    }
    return 0;
}

 

猜你喜欢

转载自www.cnblogs.com/RootVount/p/10765180.html
Nim