POJ 1740 A New Stone Game

Source: Internet
Author: User

A New Stone Game
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 5453 Accepted: 2989

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 on 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 and pile that still have stones.
For example:n=4 and the piles has (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 stones to Pile 2)
0 1 6 2 (move stones to Pile 3)
0 1 4 4 (move stones to Pile 4)
Alice always moves first. Suppose that both Alice and Bob does their best in the game.
You is to write a program to determine who'll finally win the game.

Input

The input contains several test cases. The first line of all 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 Numbe R of stones in each pile would not exceed 100.
The last test case was 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

Source

[email protected]

Analysis Citation: http://www.cnblogs.com/rainydays/archive/2011/07/09/2101918.html

Test instructions: For n heap of stones, each pile of several, two people in turn, each operation in two steps, the first step from a heap to remove at least one, the second step (can be omitted) to the heap of the remaining stones part of the other heap.

Finally, whoever has no son is preferable to lose.

Analysis: First we consider two stacks of equal situation, must be who wins who loses, because the other party can always do symmetrical operation. For four piles, 1, 2 heap equal, 3, 4 heap equal situation, must also be the first to lose, the hand also only need to do symmetrical operation (in order to take the stones in the symmetric heap of the same number of stones, and the same amount of the same as the same as the first to assign to the heap of symmetry heap. (If the initiator is in the 3 heap, and divided into 1 piles, then the right-handed in 4 heap, divided into 2 piles). In other words, for any pair of equal cases, it must be a win.

We're going to prove that in addition to all of the above, everything is winning. Because any one situation can be converted into a pair of equal cases. If the total heap count is an odd number of cases, you can assign a heap of stones up to the other heap, making the other heaps equal to 22. Up to a heap of stones is definitely enough to complete this task. Because we draw the stones from small to large to be drawn as a bar chart. Divide the adjacent two into a group (1 and 21 groups, 3 and 41 groups ...). We need to fill 1,3,5 with nth heap ... Heap we put the need to fill these gaps (2:1 higher out of the section, 4:3 higher out of the section ...). Projected to the left of the chart on the y-axis, we will find that this is some discontinuous interval, the sum of its length is significantly less than the nth heap. So it can be filled.

For cases where the number of heaps is even. We reduce the number of piles to as many as the fewest, and divide the removed stones into other heaps so that they are equal to each other. The feasibility is similar to the previous odd case.

So as long as the judge is not a pair of equal conditions can be.

1 /*by Silvern*/2#include <iostream>3#include <algorithm>4#include <cstring>5#include <cstdio>6#include <cmath>7 using namespacestd;8 BOOLf[ -];9 intCNT;Ten intnum; One intN; A intMain () { -      while(SCANF ("%d", &n) &&N) { -Memset (F,false,sizeoff); theCnt=0; -          for(intI=1; i<=n;i++){ -scanf"%d",&num); -             if(F[num]) cnt--; +             Elsecnt++; -F[num]=!f[num];//State Inversion +         } A         if(CNT) printf ("1\n"); at         Elseprintf"0\n"); -     } -     return 0; -}

POJ 1740 A New Stone Game

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.