【题解】LightOJ-1199 Partitioning Game

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.

样例输入

1
2
3
4
5
6
7
3
1
4
3
1 2 3
1
7

样例输出

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

提示

思路

SG函数打表。

代码

查看代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using namespace std;
int SG[10005];

void getSg(int n) {
for(int i=1; i<=n; i++){
bool S[10005]={0};
for(int j=1; j+j<i; j++)
S[SG[j] ^ SG[i-j]] = 1;
int mex = 0;
while(S[mex]) mex++;
SG[i] = mex;
}
}

int main()
{
getSg(10005);
int T; scanf("%d", &T);
for(int cs=1; cs<=T; cs++)
{
int n; scanf("%d", &n);

int nim = 0;
for(int i=0; i<n; i++){
int x; scanf("%d", &x);
nim ^= SG[x];
}
printf("Case %d: ", cs);
if(nim){
printf("Alice\n");
}else{
printf("Bob\n");
}
}
return 0;
}
_/_/_/_/_/ EOF _/_/_/_/_/