LightOJ 1199 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

题意

有n堆石子,双方轮流进行操作,Alice先手,每次操作可以把任意一堆石子分成两堆不同的石子,谁不能进行操作谁输

思路

这题符合用SG的要求,所以可以先打出一堆为10000的石子的SG值然后异或即可,那么怎么进行操作呢,因为这个不是把石子取走,所以每次操作会留下两个后继状态,这两个后继状态同样可以用SG表示所以上一状态的SG值就为两个后继状态的SG异或值,对于一堆有i个石子的状态他的后继状态可以由
{ ( 1 , 1 − i ) , ( 2 , i − 2 ) , ( 3 , i − 3 ) ⋯ ( ( i − 1 ) / 2 , i − ( i − 1 ) / 2 ) } \{ (1,1-i),(2,i-2),(3,i-3)\cdots((i-1)/2,i-(i-1)/2)\} {(1,1i),(2,i2),(3,i3)((i1)/2,i(i1)/2)}
所以有
S G ( x ) = m e x { S G ( 1 ) ∧ S G ( x − 1 ) , S G ( 2 ) ∧ S G ( x − 2 ) , . . . , S G ( ( x − 1 ) / 2 ) ∧ S G ( x − ( x − 1 ) / 2 ) } SG(x) = mex\{ SG(1)\wedge SG(x-1), SG(2)\wedge SG(x-2),..., SG((x-1)/2)\wedge SG(x-(x-1)/2)\} SG(x)=mex{SG(1)SG(x1),SG(2)SG(x2),...,SG((x1)/2)SG(x(x1)/2)}

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
int sg[10005],vis[10005];
void getSG()
{
    int i,j;
    memset(sg,0,sizeof(sg));
    for(i=1; i<=10000; i++)
    {
        memset(vis,0,sizeof(vis));
        for(j=1; j*2<i;j++)
            vis[sg[i-j]^sg[j]]=1;
        for(j=0; j<=10000; j++)
        {
            if(vis[j]==0)
            {
                sg[i]=j;
                break;
            }
        }
    }
}
int main()
{
    getSG();
    int t,cas=1;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        int ans=0;
        for(int i=0;i<n;i++)
        {
            int x;
            scanf("%d",&x);
            ans^=sg[x];
        }
        printf("Case %d: ",cas++);
        if(ans==0)
            printf("Bob\n");
        else
            printf("Alice\n");
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值