Partitioning Game (SG函数)

 

原题目:

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

 

中文概要:

几堆石头,可以拿选一堆石头拿一个以上的石头数,或者将这一堆分成两堆,问最后谁赢

SG函数,对于每一堆石头,后继状态有{1,n-1},{2,n-2},.....然后直接算SG函数就行了

#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*2<i;j++)
        {
            if(i!=j*2)
            {
                vis[SG[j]^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 cas=1;cas<=test;cas++)
    {
        int n;
        scanf("%d",&n);
        int ans=0;
        for(int i=1;i<=n;i++)
        {
            int x;
            scanf("%d",&x);
            ans^=SG[x];
        }
        if(ans==0) printf("Case %d: Bob\n",cas);
        else printf("Case %d: Alice\n",cas);
    }
    return 0;
}

上面第一个函数就是求SG函数的模板

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

deebcjrb

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值