Light OJ 1199 - Partitioning Game (博弈sg函数)

本文介绍了一种基于博弈论的游戏算法实现,通过分析特定游戏规则,利用SG定理解决复杂博弈问题,确定游戏的最优策略及赢家。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

D - Partitioning Game
Time Limit:4000MS      Memory Limit:32768KB      64bit IO Format:%lld & %llu
Submit  Status

Description

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堆石子(1<=n<=100),每一堆分别有xi个石子(1<=xi<=10000),
   一次操作可以使一堆石子变成两堆数目不相等的石子,
   最后不能操作的算输,问先手胜还是后手胜。
思路:n堆石子相互独立,所以可以应用SG定理,只需要算出一堆石子的SG函数。
   一堆石子(假设有x个)的后继状态可以枚举出来,分别是{1,x-1},{2,x-2},...,{(x-1)/2,x-(x-1)/2},
   一堆石子分成的两堆石子又相互独立,再次应用SG定理。

   所以SG(x) = mex{ SG(1)^SG(x-1), SG(2)^SG(x-2),..., SG((x-1)/2)^SG(x-(x-1)/2) },

   最后的答案是SG(x1)^SG(x2)^...^SG(xn)


 

#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
int hash[10005],sg[10005];
void getsg()  //获得sg函数的模板
{
    memset(sg,0,sizeof(sg));
    for(int i=1;i<=10000;i++)
    {
        memset(hash,0,sizeof(hash));
        for(int j=1;j+j<i;j++)
        hash[sg[j]^sg[i-j]]++;
        for(int j=0;j<=10000;j++)
        if(!hash[j])
        {
            sg[i]=j;
            break;
        }
    }
}
int main()
{
    getsg();
    int i,n,t,cas=1;
    cin>>t;
    while(t--)
    {
        cin>>n;
        int ans=0,data;
        for(i=0;i<n;i++)
        {
            cin>>data;
            ans^=sg[data]; //常规尼姆异或
        }
        if(ans)
        printf("Case %d: Alice\n",cas++);
        else
        printf("Case %d: Bob\n",cas++);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值