HDU3032 Nim or not Nim?

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3032

Nim or not Nim?

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

题解

标准的Lasker’s博弈

将一堆石子分成若干堆就相当于重开了两个游戏,原游戏的 SG S G 值等于分裂出去的两个游戏的 SG S G 值得异或和,我们可以通过将各个 SG S G 值异或在一起来判定胜负, SG S G 为0先手必败,反之先手必胜。

然而事实是直接求 SG S G 的复杂度爆炸,我们选择打表找规律:

sg[0]=0
sg[1]=1
sg[2]=2
sg[3]=4
sg[4]=3
sg[5]=5
sg[6]=6
sg[7]=8
sg[8]=7
sg[9]=9
sg[10]=10
sg[11]=12
sg[12]=11
sg[13]=13
sg[14]=14
sg[15]=16
sg[16]=15
sg[17]=17
sg[18]=18
sg[19]=20
sg[20]=19
sg[21]=21
sg[22]=22
sg[23]=24
sg[24]=23
sg[25]=25
sg[26]=26
sg[27]=28
sg[28]=27
sg[29]=29
sg[30]=30
sg[31]=32
sg[32]=31
sg[33]=33
sg[34]=34
sg[35]=36
sg[36]=35
sg[37]=37
sg[38]=38
sg[39]=40
sg[40]=39
sg[41]=41
sg[42]=42
sg[43]=44
sg[44]=43
sg[45]=45
sg[46]=46
sg[47]=48
sg[48]=47
sg[49]=49
sg[50]=50
sg[51]=52
sg[52]=51
sg[53]=53
sg[54]=54
sg[55]=56
sg[56]=55
sg[57]=57
sg[58]=58
sg[59]=60
sg[60]=59
sg[61]=61
sg[62]=62
sg[63]=64
sg[64]=63
sg[65]=65
sg[66]=66
sg[67]=68
sg[68]=67
sg[69]=69
sg[70]=70
sg[71]=72
sg[72]=71
sg[73]=73
sg[74]=74
sg[75]=76
sg[76]=75
sg[77]=77
sg[78]=78
sg[79]=80
sg[80]=79
sg[81]=81
sg[82]=82
sg[83]=84
sg[84]=83
sg[85]=85
sg[86]=86
sg[87]=88
sg[88]=87
sg[89]=89
sg[90]=90
sg[91]=92
sg[92]=91
sg[93]=93
sg[94]=94
sg[95]=96
sg[96]=95
sg[97]=97
sg[98]=98
sg[99]=100
sg[100]=99

发现当 n n 为4的倍数时SG[n]=n1,当 n mod 4=3 n   m o d   4 = 3 时, SG[n]=n+1 S G [ n ] = n + 1

愉快AC

代码

打表:

#include<bits/stdc++.h>
using namespace std;
const int M=1e5;
int sg[M];
int g(int x)
{
    if(sg[x]>=0)return sg[x];
    bool mex[105];int hh=0;
    memset(mex,0,sizeof(mex));
    for(int i=0;i<x;++i)
    mex[g(i)]=1;
    for(int i=x/2;i>=1;--i)
    {
        hh=g(i);
        hh^=g(x-i);
        mex[hh]=1;
    }
    for(int i=0;;++i)
    if(!mex[i])return sg[x]=i;
}
int main()
{
    memset(sg,-1,sizeof(sg));
    for(int i=0;i<=100;++i)
    printf("sg[%d]=%d\n",i,g(i));
}

正解:

#include<bits/stdc++.h>
using namespace std;
int n,x;
void ac()
{
    int ans=0;
    scanf("%d",&n);
    for(int i=1;i<=n;++i)
    {
        scanf("%d",&x);
        if(x%4==0)ans^=(x-1);
        else if(x%4==3)ans^=(x+1);
        else ans^=x;
    }
    if(ans)printf("Alice\n");
    else printf("Bob\n");
}
int main()
{
    int T;
    scanf("%d",&T);
    for(int i=1;i<=T;++i)ac();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

ShadyPi

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

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

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

打赏作者

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

抵扣说明:

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

余额充值