HDU 3032 Nim or not Nim?(博弈 SG打表找规律)

227 篇文章 0 订阅
23 篇文章 0 订阅

传送门


Nim or not Nim?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1457    Accepted Submission(s): 720


Problem Description
Nim is a two-player mathematic game of strategy in which players take turns removing objects from distinct heaps. On each turn, a player must remove at least one object, and may remove any number of objects provided they all come from the same heap.

Nim is usually played as a misere game, in which the player to take the last object loses. Nim can also be played as a normal play game, which means that the person who makes the last move (i.e., who takes the last object) wins. This is called normal play because most games follow this convention, even though Nim usually does not.

Alice and Bob is tired of playing Nim under the standard rule, so they make a difference by also allowing the player to separate one of the heaps into two smaller ones. That is, each turn the player may either remove any number of objects from a heap or separate a heap into two smaller ones, and the one who takes the last object wins.
 

Input
Input contains multiple test cases. The first line is an integer 1 ≤ T ≤ 100, the number of test cases. Each case begins with an integer N, indicating the number of the heaps, the next line contains N integers s[0], s[1], ...., s[N-1], representing heaps with s[0], s[1], ..., s[N-1] objects respectively.(1 ≤ N ≤ 10^6, 1 ≤ S[i] ≤ 2^31 - 1)
 

Output
For each test case, output a line which contains either "Alice" or "Bob", which is the winner of this game. Alice will play first. You may asume they never make mistakes.
 

Sample Input
  
  
2 3 2 2 3 2 3 3
 

Sample Output
  
  
Alice Bob
 

Source
 

题目大意:

有两个人Alice 和 Bob在玩一个游戏,游戏的内容就是一共有 m 堆石子,每个人有两种操作:
1.可以拿每一堆的任意数量但不能不拿;

2.可以将当前一堆的石子(当前的石子数肯定是>=2的)变为两堆;

最后谁先拿完谁赢,Alice 先手


解题思路:

首先我们看一下数据范围,S[i]<2^31-1,所以我们不能直接用SG函数,但是我们可以先进行打表,然后看一下有没有什么规律,首先我们分析一下,如果没有后边的第二种操作那么这就是一个简单的尼姆博弈,就是求一个异或值就行了,那么加上第二种操作,那么就考虑一下它的后继状态,他的后即状态,SG[i] = mex{i-1,i-2... SG[i-1]^SG[1],SG[i-2]^SG[2]};所以我们就可以通过这个后继状态来打一个表,通过这个表我们找到了一些规律,就是当x%4==0的时候,SG[x] = x-1,当x%4==3的时候,SG[x] = x+1,其他情况就是SG[x] = x;


My Code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 1e3+5;
int sg[MAXN];
int get_SG(int x)
{
    if(sg[x]!=-1)
        return sg[x];
    int hash[MAXN];
    memset(hash, 0, sizeof(hash));
    for(int i=x-1;i>=0;i--)
        hash[get_SG(i)]=1;
    for(int j=1; j<=x/2; j++)
        hash[get_SG(x-j)^get_SG(j)]=1;
    int k;
    for(k=0; k<MAXN; k++)
    {
        if(!hash[k])
        {
            return sg[x] = k;
        }
    }
}
int main()
{
    /**测试打表**/
    /*
    memset(sg,-1,sizeof(sg));
    for(int i=0; i<50; i++)
    {
        printf("sg[%d] = %d\n",i,get_SG(i));
    }*/
    int T;
    cin>>T;
    while(T--)
    {
        int m;
        int ans = 0;
        cin>>m;
        while(m--)
        {
            int x;
            cin>>x;
            if(x%4==0)
                ans ^= (x-1);
            else if(x%4 == 3)
                ans ^= (x+1);
            else
                ans ^= x;
        }
        if(ans)
            puts("Alice");
        else
            puts("Bob");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值