lightoj 1296 Again Stone Game(SG函数)


1296 - Again Stone Game

   PDF (English)StatisticsForum
Time Limit: 2 second(s)Memory Limit: 32 MB

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

Output for Sample Input

5

1

1

3

10 11 12

5

1 2 3 4 5

2

4 9

3

1 3 9

Case 1: Bob

Case 2: Alice

Case 3: Alice

Case 4: Bob

Case 5: Alice

 题意:n堆石头,最少拿每堆石头的一个,最多拿每堆石头的一半,问最后谁赢

SG函数打表,但是打了表我也没找出什么规律......还以为打错表了,看了看题解没错,就是没找出规律.....

规律就是当这个数是偶数的时候,它的SG值等于它除以2,如果是奇数的话,它的SG值就等于SG【x/2】

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=1e5+7;
bool vis[maxn];
int SG[maxn];
void getSG(int n)
{
    for(int i=1;i<=n;i++)
    {
        memset(vis,false,sizeof(vis));
        for(int j=1;j<=i/2;j++)
        {
            vis[SG[i-j]]=true;
        }
        for(int j=0;j<10001;j++)
        {
            if(!vis[j])
            {
                SG[i]=j;
                break;
            }
        }
    }
    return;
}
int main()
{
    int test;
    scanf("%d",&test);
    //getSG(10000);
    /*for(int i=1;i<=50;i++)
    {
        if(i%5==0)
        {
            cout<<SG[i]<<endl;
        }
        else cout<<SG[i]<<' ';
    }*/
    for(int cas=1;cas<=test;cas++)
    {
        int ans=0;
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            int x;
            scanf("%d",&x);
            if(x%2==0)
            {
                ans^=(x/2);
            }
            else
            {
                while(x%2==1)
                {
                    x/=2;
                }
                ans^=(x/2);
            }
        }
        if(ans==0) printf("Case %d: Bob\n",cas);
        else printf("Case %d: Alice\n",cas);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值