首页 > 代码库 > hdu 5011 Game
hdu 5011 Game
Game
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 263 Accepted Submission(s): 205
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.
● 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.
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
1 1 2 1 1 3 1 2 3
Sample Output
Win Lose Lose
Source
2014 ACM/ICPC Asia Regional Xi‘an Online
题解及代码:
简单粗暴的nim博弈,有一点变形就是当我们取出第i堆石子的一部分后,可以将这第i堆石子的剩下的部分分成两堆,其实这对策略的考虑是不造成任何影响的,下面来分析一下:
处于必胜状态的就不说了,因为他可以取出一部分石子使其达到必败状态(不必考虑将当前的石子分成两堆)。
假设当前所有的石子的情况处于必败状态,也就是a0^a1^a2^...^an=0,那么我们假设就取an中的一部分石子,取完之后,此时处于必胜状态,那么我们就分析下将当前的石子分成两堆,能都使得最后所有的石子数异或为0 就可以了,很简单:我们知道a0^a1^a2^...^an-1=an,因为取了an中的一部分石子之后的an‘<an,假设an’=a+b,那么a^b最大也就是an‘,不可能达到an,所以a0^a1^a2^...^an-1^a^b必然不能为0,所以这项操作是没有什么意义的,所以整体还是一个最简单的nim博弈。
#include <iostream> #include <cstdio> #include <cstring> using namespace std; int main() { int n,x; while(scanf("%d",&n)!=EOF) { int ans=0; for(int i=0;i<n;i++) { scanf("%d",&x); ans^=x; } if(ans) printf("Win\n"); else printf("Lose\n"); } return 0; }
hdu 5011 Game
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。