55 博弈(找规律);

LightOJ - 1296

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),但是这里每一堆的石子数目是非常多的,如果单个跑的话会超时,

55 博弈(找规律);_第1张图片

我们可以再小范围内进行暴力,在大范围内我们就根据小范围内的规律来推算出大范围的来,这道题目的意思是很简单的就是偶数的话sg就是除以2,如果是奇数的话就减掉1然后除以2,一直到时偶数的时候,时间复杂度是log(n)的,那么我们可以找到每一堆石子的sg值,这样的话我们就将这个博弈转换为传统尼姆博弈;

#include 
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;
}

 

你可能感兴趣的:(博弈)