首页 > ACM题库 > HDU-杭电 > HDU 3032-Nim or not Nim?-博弈-[解题报告]HOJ
2014
02-27

HDU 3032-Nim or not Nim?-博弈-[解题报告]HOJ

Nim or not Nim?

问题描述 :

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 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)

输出:

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)

样例输入:

2
3
2 2 3
2
3 3

样例输出:

Alice
Bob

题意:给定n堆石子,两人轮流操作,每次选一堆石子,取任意石子或则将石子分成两个更小的堆(非0),取得最后一个石子的为胜。

题解:比较裸的SG定理,用sg定理打表,得到表1,2,4,3,5,6,8,7,9,10,12,11…可以发现当x%4==0时sg[x]=x-1;当x%4==3时sg[x]=x+1;其余sg[x]=x。然后异或下就出来结果了。主要还是用学会SG定理。详细的可以百度,或则看我其他的文章(我自己都忘了哪篇写过。。Nim or not Nim?)

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn=1e6+10;
int find(int x)
{
    if(x%4==0)return x-1;
    else if(x%4==3)return x+1;
    return x;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int a,n,i,j,ans=0;
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
            scanf("%d",&a);
            ans=ans^find(a);
        }
        if(ans==0)printf("Bob\n");
        else printf("Alice\n");
    }
    return 0;
}

SG定理打表+找规律:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn=1e4+10;
int sg[maxn],vis[maxn];
void init()
{
    int i,j,k;
    sg[0]=0,sg[1]=1;
    for(i=2;i<=1000;i++)
    {
        memset(vis,0,sizeof(vis));
        for(j=1;j<i;j++)
        vis[sg[j]^sg[i-j]]=1;  //拆分
        for(j=0;j<i;j++)
        vis[sg[j]]=1;         //取石子
        for(j=0;;j++)
        if(!vis[j])break;
        sg[i]=j;
    }
    for(i=1;i<=20;i++)
    cout<<sg[i]<<endl;
}
int main()
{
    init();
}

参考:http://blog.csdn.net/a601025382s/article/details/13094403


  1. a是根先忽略掉,递归子树。剩下前缀bejkcfghid和后缀jkebfghicd,分拆的原则的是每个子树前缀和后缀的节点个数是一样的,根节点出现在前缀的第一个,后缀的最后一个。根节点b出现后缀的第四个位置,则第一部分为四个节点,前缀bejk,后缀jkeb,剩下的c出现在后缀的倒数第2个,就划分为cfghi和 fghic,第3部分就为c、c