HDU5011 Game(尼姆博奕)

来源:互联网 发布:java银行管理系统代码 编辑:程序博客网 时间:2024/06/08 20:07

题目:

Game

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1372    Accepted Submission(s): 891


Problem Description
Here is a game for two players. The rule of the game is described below: 

● In the beginning of the game, there are a lot of piles of beads.

● Players take turns to play. Each turn, player choose a pile i and remove some (at least one) beads from it. Then he could do nothing or split pile i into two piles with a beads and b beads.(a,b > 0 and a + b equals to the number of beads of pile i after removing) 

● If after a player's turn, there is no beads left, the player is the winner.

Suppose that the two players are all very clever and they will use optimal game strategies. Your job is to tell whether the player who plays first can win the game.
 

Input
There are multiple test cases. Please process till EOF.

For each test case, the first line contains a postive integer n(n < 105) means there are n piles of beads. The next line contains n postive integer, the i-th postive integer ai(ai < 231) means there are ai beads in the i-th pile.
 

Output
For each test case, if the first player can win the game, ouput "Win" and if he can't, ouput "Lose"
 

Sample Input
1121 131 2 3
 

Sample Output
WinLoseLose
 

Source
2014 ACM/ICPC Asia Regional Xi'an Online
 

Recommend
hujie   |   We have carefully selected several similar problems for you:  6022 6021 6020 6019 6018 
 

Statistic | Submit | Discuss | Note
思路:

尼姆博奕的模板题,出现奇异局势,一定输

关于尼姆博奕:

在了解尼姆博弈之前,先了解一下二进制中的异或运算。

所谓异或运算,就是在所有数均化为二进制之后的一种运算,并且是八位,正数不够补零,(负数不够补一),

举个例子:4的二进制是100,八位之后便是00000100;在此基础上介绍异或运算,异或运算又名(+),为什么这样说呢,因为它只加却不进位,比如7异或3,即111(+)011,结果是100,就是1(+)1=0,1(+)0=1;独特的加法运算。

接下来便是尼姆博弈,有n堆石子,每次可以取一堆,或者是一堆中的一部分,谁先取完谁胜。

这里提到奇异局势,就是无论哪一方面对奇异局势最终都会败。比如当n=3时,描述其中一种奇异局势(0,0,0);

是不是谁面对它都必败(嘿嘿,咱继续),(0,x,x)也是,信不?我取x,他取x,然后我败,,也就是说,谁面对奇异局势,必败。

这个时候,为了胜出,所以就要千方百计的让对方面对奇异局势。

也就是你要在这堆里取某个特定的数目,使其变为奇异局势。假设这个数目为p;

怎么求p呢,这前人的智慧就显得至关重要了,

尼姆老大爷就给我们总结了:

任何奇异局势(a,b,c)都有a (+)b(+) c =0。假设要在c中取石子,那就p=c--a(+)b,(如果c<p,那就换从a或者b中取呗),在此之前,有必要了解一点,一个数异或它自身,结果是0.


代码:

#include <cstdio>#include <cstring>#include <cctype>#include <string>#include <set>#include <iostream>#include <stack>#include <cmath>#include <queue>#include <vector>#include <algorithm>#define mem(a,b) memset(a,b,sizeof(a))#define inf 0x3f3f3f3f#define N 100000+20#define mod 10007#define M 1000000+10#define ll long longusing namespace std;int main(){    ll n;    while(~scanf("%lld",&n))    {        ll sum=0,x,flag=0;        for(ll i=0; i<n; i++)        {            scanf("%lld",&x);            sum^=x;            if(sum==0)//出现奇异局势一定输            {                flag=1;                break;            }        }        if(flag)            puts("Lose");        else            puts("Win");    }    return 0;}


0 0
原创粉丝点击