Alice and Bob are playing a strange game. The rules of the game are:
-
Initially there are n piles.
-
A pile is formed by some cells.
-
Alice starts the game and they alternate turns.
-
In each tern a player can pick any pile and divide it into two unequal piles.
-
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
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.
Output
For each case, print the case number and ‘Alice’ or ‘Bob’ depending on the winner of the game.
Sample Input
3
1
4
3
1 2 3
1
7
Sample Output
Case 1: Bob
Case 2: Alice
Case 3: Bob
题意
有n堆石子,双方轮流进行操作,Alice先手,每次操作可以把任意一堆石子分成两堆不同的石子,谁不能进行操作谁输
思路
这题符合用SG的要求,所以可以先打出一堆为10000的石子的SG值然后异或即可,那么怎么进行操作呢,因为这个不是把石子取走,所以每次操作会留下两个后继状态,这两个后继状态同样可以用SG表示所以上一状态的SG值就为两个后继状态的SG异或值,对于一堆有i个石子的状态他的后继状态可以由
{
(
1
,
1
−
i
)
,
(
2
,
i
−
2
)
,
(
3
,
i
−
3
)
⋯
(
(
i
−
1
)
/
2
,
i
−
(
i
−
1
)
/
2
)
}
\{ (1,1-i),(2,i-2),(3,i-3)\cdots((i-1)/2,i-(i-1)/2)\}
{(1,1−i),(2,i−2),(3,i−3)⋯((i−1)/2,i−(i−1)/2)}
所以有
S
G
(
x
)
=
m
e
x
{
S
G
(
1
)
∧
S
G
(
x
−
1
)
,
S
G
(
2
)
∧
S
G
(
x
−
2
)
,
.
.
.
,
S
G
(
(
x
−
1
)
/
2
)
∧
S
G
(
x
−
(
x
−
1
)
/
2
)
}
SG(x) = mex\{ SG(1)\wedge SG(x-1), SG(2)\wedge SG(x-2),..., SG((x-1)/2)\wedge SG(x-(x-1)/2)\}
SG(x)=mex{SG(1)∧SG(x−1),SG(2)∧SG(x−2),...,SG((x−1)/2)∧SG(x−(x−1)/2)}
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
int sg[10005],vis[10005];
void getSG()
{
int i,j;
memset(sg,0,sizeof(sg));
for(i=1; i<=10000; i++)
{
memset(vis,0,sizeof(vis));
for(j=1; j*2<i;j++)
vis[sg[i-j]^sg[j]]=1;
for(j=0; j<=10000; j++)
{
if(vis[j]==0)
{
sg[i]=j;
break;
}
}
}
}
int main()
{
getSG();
int t,cas=1;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
int ans=0;
for(int i=0;i<n;i++)
{
int x;
scanf("%d",&x);
ans^=sg[x];
}
printf("Case %d: ",cas++);
if(ans==0)
printf("Bob\n");
else
printf("Alice\n");
}
return 0;
}