1. 程式人生 > >55 博弈(找規律);

55 博弈(找規律);

Alice and Bob are playing a stone game. Initially there are n piles of stones and each pile contains some stone. Alice stars the game and they alternate moves. In each move, a player has to select any pile and should remove at least one and no more than half stones from that pile. So, for example if a pile contains 10 stones, then a player can take at least 1 and at most 5 stones from that pile. If a pile contains 7 stones; at most 3 stones from that pile can be removed.

Both Alice and Bob play perfectly. The player who cannot make a valid move loses. Now you are given the information of the piles and the number of stones in all the piles, you have to find the player who will win if both play optimally.

Input

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

Each case starts with a line containing an integer n (1 ≤ n ≤ 1000). The next line contains n space separated integers ranging in [1, 109]. The ith integer in this line denotes the number of stones in the ith pile.

Output

For each case, print the case number and the name of the player who will win the game.

Sample Input

5

1

1

3

10 11 12

5

1 2 3 4 5

2

4 9

3

1 3 9

Sample Output

Case 1: Bob

Case 2: Alice

Case 3: Alice

Case 4: Bob

Case 5: Alice

這道題目的意思很簡單就是一個取石子的博弈,但是取得方式根傳統的取得方式是不同的,這裡取得上限是有區別的,這裡不是拿走一堆,而是一次最多拿走這一堆石子的一半(/2)取整,好這裡的後繼狀態找到了(sg),但是這裡每一堆的石子數目是非常多的,如果單個跑的話會超時,

我們可以再小範圍內進行暴力,在大範圍內我們就根據小範圍內的規律來推算出大範圍的來,這道題目的意思是很簡單的就是偶數的話sg就是除以2,如果是奇數的話就減掉1然後除以2,一直到時偶數的時候,時間複雜度是log(n)的,那麼我們可以找到每一堆石子的sg值,這樣的話我們就將這個博弈轉換為傳統尼姆博弈;

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int Max = 1e3+10;
const ll inf = 1e18;

#define rep(i,s,n) for(ll i=s;i<=n;i++)
#define per(i,n,s) for(ll i=n;i>=s;i--)

int sg[Max];
//bool visited[Max];
//void pri(){
//    for(int i=1;i<=100;i++){
//        memset(visited,0,sizeof(visited));
//        for(int j=1;j<=i/2;j++){
//            visited[sg[i-j]]=1;
//        }
//        for(int j=0;;j++){
//            if(!visited[j]){
//                sg[i]=j;
//                break;
//            }
//        }
//    }
//}
int main(){
//    pri();
//    for(int i=0;i<40;i++){
//        printf("%d %d\n",i,sg[i]);
//    }
      int t;
      scanf("%d",&t);
      int ca=1;
      while(t--){
        int n;
        scanf("%d",&n);
        ll x;
        ll key=0;
        for(int i=1;i<=n;i++){
            scanf("%lld",&x);
            while(1){
                if(x%2==0) break;
                else{
                    x--;
                    x/=2;
                }
            }
            key^=x/2;
        }
        printf("Case %d: ",ca++);
        if(key){
        printf("Alice\n");
        }
        else {
        printf("Bob\n");
        }
      }
   return 0;
}