Description
Alice and Bob decide to play a new stone game.At the beginning of the game they pick n(1<=n<=10) piles of stones in a line. Alice and Bob move the stones in turn.
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
The 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
For each test case, if Alice win the game,output 1,otherwise output 0.
Sample Input
3
2 1 3
2
1 1
0
Sample Output
1
0
Source
题意:有n堆不为0的石子,现在2个人进行操作,
每次操作:选择一堆石子,然后把这堆石子扔掉至少一个,然后这堆石子剩余的石子,可以选择拿出其中的0~全部石子,拿出的石子可以分给其余的不为0的堆,可以分给一堆,也可以分给若干堆。
要是某一方没有石子扔了,则该方失败。
输出:先手赢则输出1,先手输则输出0.
分析:
n=1,则先手必胜。
n=2,若a0==a1,则先手必败,因为无论先手进行什么操作,后手都可以做对称的操作,最后一定是先手先没有石子可以拿了。
n=3,设a0<=a1<=a2,则先手必胜,因为有a1-a0<a2,所以先手可以把a2拿出a1-a0给a0,其余扔掉。
则此时a0==a1,就把必败态留给了后手,所以先手必胜。
n=4,设a0<=a1<=a2<=a3,若a0=a1=a2=a3,或a0=a1和a2=a3,则先手必败,因为后手可以和先手做同样的操作。
除了这2种情况,先手必胜,因为:a3-a0>a2-a1,所以可以从a3中拿出a3-a0的石子给a1,使得a1=a2,剩下的扔掉,这样操作后就有a3=a0,a1=a2,这样就把必败态留给了后手,所以先手必胜。
n=2*k-1时,设a1<=...<=an,(下标从1开始了)则有:an>a2-a1+a4-a3+..+an-1-an-2,(数据都放到不等号一边就发现了),
所以可以把堆an补给a1,a3,a5,...,an-2,剩下的扔掉,这样使得有a1=a2,a3=a4,...an-2=an-1,
接下来后手做什么先手都可以做对称的操作,则就是把必败态留给了后手,先手必胜。
n=2*k时,同理:若a1=a2,a3=a4,...an-1=an,则先手必败,因为先手做什么后手都可以做对称的操作。
否则的话,可以从堆an中取出an-a1石子,分别给a2,a4,...an-2,使得有an=a1,an-1=an-2,...a3=a2,这样就把必败态留给了后手,先手必胜。
至此,可以得出结论了,
n=2*k-1时,先手必胜。
n=2*k时,若石子两两相同,则先手必败,否则先手必胜。
1 #include<cstdio>
2 #include<algorithm>
3 using namespace std;
4
5 const int maxn=13;
6
7 int a[maxn];
8
9 int main()
10 {
11 int n;
12 while(scanf("%d",&n))
13 {
14 if(!n)
15 break;
16 for(int i=0;i<n;i++)
17 scanf("%d",&a[i]);
18
19 if(n%2)
20 printf("1\n");
21 else
22 {
23 bool flag=true;
24
25 sort(a,a+n);
26
27 for(int i=0;i<n-1;i+=2)
28 {
29 if(a[i]!=a[i+1])
30 {
31 flag=false;
32 break;
33 }
34 }
35 if(flag)
36 printf("0\n");
37 else
38 printf("1\n");
39 }
40
41 }
42
43 return 0;
44 }
View Code