1. 程式人生 > >LightOJ 1253 Misere NIM(反NIM博弈)

LightOJ 1253 Misere NIM(反NIM博弈)

tar less sample aps pty its 其他 who ins

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博弈板題;(anti-NIM博弈見前面博弈總結)知道:先手必勝的狀態有兩種: 1:全是一個,且ans=0;

2 : 不全是一個,且ans!=0;(ans為所有的異或和);

其他的情況均為先手必敗;

參考代碼:

技術分享圖片
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int T,n,x,ans,cnt;
 4 int main()
 5 {
 6     scanf("%d",&T);
 7     for(int cas=1;cas<=T;cas++)
 8     {
 9         scanf("%d",&n);
10         cnt=ans=0;
11         for(int i=1;i<=n;++i)
12         {
13             scanf("%d",&x);
14             if(x==1) cnt++;
15             ans^=x;    
16         }    
17         if(cnt==n)
18         {
19             if(ans==0) printf("Case %d: Alice\n",cas);
20             else  printf("Case %d: Bob\n",cas);
21         } 
22         else
23         {
24             if(ans) printf("Case %d: Alice\n",cas);
25             else printf("Case %d: Bob\n",cas);
26         }
27     } 
28     
29     
30     return 0;
31 }
View Code

LightOJ 1253 Misere NIM(反NIM博弈)