Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 5338 | Accepted: 2926 |
Description
At each step of the game,the player choose a pile,remove at least
one stones,then freely move stones from this pile to any other pile that
still has stones.
For example:n=4 and the piles have (3,1,4,2) stones.If the player
chose the first pile and remove one.Then it can reach the follow states.
2 1 4 2
1 2 4 2(move one stone to Pile 2)
1 1 5 2(move one stone to Pile 3)
1 1 4 3(move one stone to Pile 4)
0 2 5 2(move one stone to Pile 2 and another one to Pile 3)
0 2 4 3(move one stone to Pile 2 and another one to Pile 4)
0 1 5 3(move one stone to Pile 3 and another one to Pile 4)
0 3 4 2(move two stones to Pile 2)
0 1 6 2(move two stones to Pile 3)
0 1 4 4(move two stones to Pile 4)
Alice always moves first. Suppose that both Alice and Bob do their best in the game.
You are to write a program to determine who will finally win the game.
Input
input contains several test cases. The first line of each test case
contains an integer number n, denoting the number of piles. The
following n integers describe the number of stones in each pile at the
beginning of the game, you may assume the number of stones in each pile
will not exceed 100.
The last test case is followed by one zero.
Output
Sample Input
3
2 1 3
2
1 1
0
Sample Output
1
0
Source
【思路】
博弈
当n=1时,先手必胜;
当n=2时,如果两堆相等,先手取后只剩一堆则局面必胜,先手取后剩两堆,则后手可以始终维持两堆相等直到(1,1),此时必由先手完成第一种情况。因此先手必败。
当n=3时,若a1<=a2<=a3,则先手可以通过操纵a3使得剩下两堆为(a2,a2),此时局面必败。因此先手必胜。
推广:
当n为奇数时,若a1<=a2…<=an-1<=an,则操作an使得a1->a2,a3->a4….an-2->an-1,因为项为正,所以an>=a2-a1+…an-1-an-2。因此先手必胜。
当n为偶数时:若a1=a2,a3=a4…an-2=an-1,则先手必败;否则总可以操作an使得an=a1,a2=a3…an-2=an-1,an-a1>=a2-a2+…an-1-an-2,此时先手必胜。
【代码】
#include<cstdio>
#include<algorithm>
using namespace std; int n,a[]; int main() {
while(scanf("%d",&n)== && n) {
for(int i=;i<=n;i++) scanf("%d",&a[i]);
if(n&) puts("");
else {
sort(a+,a+n+);
int flag=;
for(int i=;i<=n;i+=)
if(a[i]!=a[i+]) { flag=; break; }
if(flag) puts(""); else puts("");
}
}
return ;
}