Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 5027 | Accepted: 2752 |
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
The last test case is followed by one zero.
Output
Sample Input
3 2 1 3 2 1 1 0
Sample Output
1 0
題意:對於n堆石子,每堆若干個,兩人輪流操作,每次操作分兩步,第一步從某堆中去掉至少一個,第二步(可省略)把該堆剩餘石子的一部分分給其它的某些堆。
最後誰無子可取即輸。
分析:
只有一堆時先手必勝。
有兩堆時若兩堆相等則後手只用和先手一樣決策即可保證勝利,後手必勝。若不同則先手可以使其變成相等的兩堆,先手必勝。
有三堆時先手只用一次決策即可將其變成兩堆相等的局面,先手必勝。
有四堆時由於三堆必勝,無論先手後手都想逼對方取完其中一堆,而只有在四堆都爲一顆時纔會有人取完其中一堆,聯繫前面的結論可以發現,只有當四堆可以分成兩兩相等的兩對時先手纔會失敗。
分析到這裏,題目好像已經有了一些眉目了,憑藉歸納猜想,我們猜測必敗態的條件爲“堆數爲偶數(不妨設爲2N),並且可以分爲兩兩相等的N對”。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int Hash[110];
int main(){
int n;
int ans;
int a;
while(scanf("%d",&n)&&n){
ans=0;
memset(Hash,0,sizeof(Hash));
for(int i=0;i<n;i++){
scanf("%d",&a);
if(Hash[a]){
Hash[a]=0;
ans++;
}
else
Hash[a]=1;
}
//cout<<ans<<' '<<n/2<<endl;
if(n%2==0&&ans==n/2)
cout<<0<<endl;
else
cout<<1<<endl;
}
return 0;
}