POJ 1740 A New Stone Game

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

 
 
 
思路:
  菜鸡太弱了,完全不会这题,想着拿SG函数写一下。。。。哎,不说了。
  Nim博弈论,主要是要找到必败的条件。那么我们一定知道,只有一堆,则后手必败。
  如果有两堆,且相等,那么先手必败。因为先手肯定不愿意把一堆全部拿完,拿完了就输了。而此时,无论先手怎么拿,后手都可以把两堆石子补到相等。(直接把多的那一部分丢了就行了),先手不得不再次操作,而后手继续补到相等,然后几次之后,石子堆将会来到尴尬的1:1,先手就只好把一堆拿完,然后认输了。所以两堆且相等是先手必败。  如果两堆不相等,先手应该马上补齐。此时再重复两堆相等的过程,此时,后手必败。
  如果有三堆石子,直接拿走一堆,把其他两组补齐,先手必胜。
  接下来,我们直接看有n堆石子,当n为偶数,并且成对出现。
 

 

就是假装这个样子,然后,先手动手了,他进行了操作:

p是去掉的,因为至少丢掉一个石子,那么p>m+n,m,n是两堆的差呀,所以如果没有补在一堆上面,这个就更满足了。

那么后手一定可以再次把他们补齐

不久之后,就会变成 1 1 1 1 1 1 1 1

然后就完了。。。。

如果是单数,那么先手一定是可以去掉一堆并补齐的,所以这也可以解释,为什么先手在之前的情况下,为什么不去掉一堆,因为对结果没有影响,反正最后会变成1 1 的。

所以,判断一下是不是单数堆,如果不是,在判断是不是两堆对应出现的就可以了。

/xyx

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int n;int a[15];
    while(cin>>n&&n){
        for(int i=0;i<n;i++){
            cin>>a[i];
        }
        sort(a,a+n);
        if(n&1){cout<<1<<endl;continue;}

        int flag=0;
        for(int i=0;i<n;i+=2){
            if(a[i]!=a[i+1]){flag=1;break;}
        }
        if(flag){cout<<1<<endl;}
        else cout<<0<<endl;
    }
}

 

 

posted @ 2018-05-22 17:17  断腿三郎  阅读(185)  评论(0编辑  收藏  举报