BZOJ 1982: [Spoj 2021]Moving Pebbles

1982: [Spoj 2021]Moving Pebbles

Time Limit:10 Sec  Memory Limit: 64 MB
Submit: 211  Solved: 142
[Submit][Status][Discuss]

Description

2021. Moving Pebbles Two players play the following game. At the beginning of the game they start with n (1<=n<=100000) piles of stones. At each step of the game, the player chooses a pile and remove at least one stone from this pile and move zero or more stones from this pile to any other pile that still has stones. A player loses if he has no more possible moves. Given the initial piles, determine who wins: the first player, or the second player, if both play perfectly. 给你N堆Stone,两个人玩游戏. 每次任选一堆,首先拿掉至少一个石头,然后移动任意个石子到任意堆中. 谁不能移动了,谁就输了...

Input

Each line of input has integers 0 < n <= 100000, followed by n positive integers denoting the initial piles.

Output

For each line of input, output "first player" if first player can force a win, or "second player", if the second player can force a win.

Sample Input

3 2 1 3



Sample Output

first player


博弈类问题的关键,就是寻找平衡状态。
这题的平衡状态是什么?石头堆的数量是成对出现的,也就是说后取者总可以模仿先取者。

逐一分析:
剩余1堆石头:先取者全部拿走,胜利。
剩余2堆石头:假设两堆石头的数量相等,均为m。对于(m,m),先取者没办法一次将两堆石头拿走,则后取者总可以模仿先取者的动作,只需换一下操作对象,使两堆石头变成(n,n)。最后肯定有某一次先取者操作后石头只剩下一堆,这时后取者将剩下的一堆全部拿走,胜利。
也就说,对于(m,m),后取者必胜。
如果两堆石头不相等,(m,n),则先取者把多的那一堆拿走一些,使两堆相等(n,n)。
也就说,对于(m,n),先取者必胜。
剩余3堆石头:(x,y,z)其中(x<=y<=z),先取者可以把z分(y-x)到x上,剩下的全部拿走,于是局面变成了(y,y,0)。也即是说对于3堆石头,先取者必胜。
对于4堆石头:如果(m,m,n,n),这样和两堆石头的(m,m)局面是类似的,先取者必败,后取者必胜。但如果不是这样的情况,(a,b,c,d)其中(a<=b<=c<=d),先取者操作最后一堆石头,总可以使局面变成(a,c,c,a)。这样就把必败局留给了后取者,也就是说这种情况先取者必胜。
对于n堆石头,分析和前面类似。。。。。

总的来说,对于n堆石头:
如果n是奇数,先取者必胜。
如果n是偶数,石头堆排序后可以写成(a,a,b,b,c,c,d,d,....)则先取者必败,否则先取者必胜。


不粘代码了


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值