码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 5011 Game

时间:2014-09-15 19:30:49      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   io   os   java   ar   

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.
 

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
1 1 2 1 1 3 1 2 3
 

Sample Output
Win Lose Lose
 

Source
 


题解及代码:


      简单粗暴的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

标签:des   style   blog   http   color   io   os   java   ar   

原文地址:http://blog.csdn.net/knight_kaka/article/details/39296601

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!