LightOJ1199 Partition Game

LightOJ1199 Partition Game

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

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

题解:对于每一个数下一个拆分为 (1,n-1),(2,n-2) ...  个

SG函数推到即可;

参考代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int maxn=10010;
 5 int T,n,SG[maxn],x,ans,vis[maxn]; 
 6 void getSG(int n)
 7 {
 8     for(int i=1;i<=n;++i)
 9     {
10         memset(vis,0,sizeof vis);
11         for(int j=1;j*2<i;++j) if(i!=j*2) vis[SG[j]^SG[i-j]]=1;
12         for(int j=0;j<maxn;++j){ if(!vis[j]) { SG[i]=j;break; } }
13     }
14 }
15 int main()
16 {
17     scanf("%d",&T);
18     getSG(maxn);
19     for(int cas=1;cas<=T;++cas)
20     {
21         scanf("%d",&n);ans=0;
22         for(int i=1;i<=n;++i)
23         {
24             scanf("%d",&x);
25             ans^=SG[x];
26         }
27         if(ans) printf("Case %d: Alice\n",cas);
28         else printf("Case %d: Bob\n",cas);
29     }
30     return 0;    
31 } 
View Code
posted @ 2019-02-16 18:59 SongHL 阅读( ...) 评论( ...) 编辑 收藏

猜你喜欢

转载自blog.csdn.net/song_hai_lei/article/details/88404649