Partitioning Game(LightOJ-1199)

题面

Alice and Bob are playing a strange game. The rules of the game are:

  1. Initially there are n piles.
  2. A pile is formed by some cells.
  3. Alice starts the game and they alternate turns.
  4. In each tern a player can pick any pile and divide it into two unequal piles.
  5. If a player cannot do so, he/she loses the game.

Now you are given the number of cells in each of the piles, you have to find the winner of the game if both of them play optimally.

输入

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

Each case starts with a line containing an integer n (1 ≤ n ≤ 100). The next line contains n integers, where the ith integer denotes the number of cells in the ith pile. You can assume that the number of cells in each pile is between 1 and 10000.

输出

For each case, print the case number and ‘Alice’ or ‘Bob’ depending on the winner of the game.

样例输入

13
21
34
43
51 2 3
61
77

样例输出

1Case 1: Bob
2Case 2: Alice
3Case 3: Bob

提示

思路

SG函数打表。

代码

 1using namespace std;
 2int SG[10005];
 3
 4void getSg(int n) {
 5    for(int i=1; i<=n; i++){
 6        bool S[10005]={0};
 7        for(int j=1; j+j<i; j++)
 8            S[SG[j] ^ SG[i-j]] = 1;
 9        int mex = 0;
10        while(S[mex]) mex++;
11        SG[i] = mex;
12    }
13}
14
15int main()
16{
17    getSg(10005);
18    int T; scanf("%d", &T);
19    for(int cs=1; cs<=T; cs++)
20    {
21        int n; scanf("%d", &n);
22
23        int nim = 0;
24        for(int i=0; i<n; i++){
25            int x; scanf("%d", &x);
26            nim ^= SG[x];
27        }
28        printf("Case %d: ", cs);
29        if(nim){
30            printf("Alice\n");
31        }else{
32            printf("Bob\n");
33        }
34    }
35    return 0;
36}