Game Theory 5

Source: Internet
Author: User
A new stone game

 

Time limit:1000 ms   Memory limit:30000 K
Total submissions:4177   Accepted:2227

 

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

 

32 1 321 10

 

Sample output

 

10

 

Analysis:

Understanding of the meaning of the question: ① After deleting an element, it can be left blank; ② whoever gets the stone, it will win; ③ the examples in the various moving methods of the stone are very clear.

Train of Thought Analysis: first look at the example:

 

() The first hand gets one, the opponent gets one, and the first hand loses (1, 2) the first hand can get one, leaving it to the opponent (), then the first hand wins (1, 3) the first hand gets two, leave it to the opponent (), then the first hand wins... (), the first hand gets one, and if it moves (), the opponent gets it, the first hand loses; if it does not move, the opponent gets the same number () in the other stack, and the first hand also loses (). The first hand gets the same number ), the rule of winning the first hand is obvious. If the number of heap is the same, the first hand loses. Otherwise, the first hand wins three? (, 2) if the first hand gets 2, then (), the first hand wins (, 3) the first hand can get 2 in heap 3, then move one to the first heap, get (2, 2), first hand win... visible, three heap, first hand win. From the above rule, we can boldly guess that the first hand wins the odd heap? For example, (, 5), you can assign 5 to the first four and change it to (,) ② an even number heap. If a1 = A2, A3 = A4, ............ An-1 = An, the first hand is lost (because it can be divided into two places to solve); otherwise, the first hand wins for example (1, 2, 3, 4, 5, 6) can be allocated to 2 ~ 5, 6 turn into 1, the overall change is (, 3, 3, 5), the first hand wins

 1 # include<stdio.h> 2 # include<algorithm> 3 using namespace std; 4 int main() 5 { 6     int n,i; 7     int sign[12]; 8     int leap; 9     while(scanf("%d",&n)&&n)10     {11     for(i=0;i<n;i++)12         scanf("%d",&sign[i]);13     if(n%2==1)14     {15         printf("1\n");16         continue;17     }18     sort(sign,sign+n);19     leap=0;20     for(i=0;i<n;i+=2)21     {22         if(sign[i]!=sign[i+1])23         {24             leap=1;25             break;26         }27     }28     if(leap)29         printf("1\n");30     else printf("0\n");31     }32     return 0;33 }

 

Game Theory 5

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.