[USTC 1213] stone game, ustc1213

Source: Internet
Author: User

[USTC 1213] stone game, ustc1213
Description

In the combined game theory, Nim games are a very classic problem. The Nim games can be described as follows:
There are n piles of stones, each of which is a1, a2 ,..., An (ai ≥ 0 ). Now the two take stones from the n heaps in turn, each time they must take any number of stones from a pile, at least one, must take stones from the same pile, and cannot exceed the total number of stones in this pile. If one party has no stone, then he will lose.
For example:

There are 3 piles of stones, respectively 3, 2, 2, A and B take turns.
A gets one from 2nd heap, and B gets three from 1st heap. At this time, the number of stones is 0, 1, and 2 respectively.
A gets one from the first heap, and then B gets one from the second heap. At this time, the number of stones is 0, 0, and 1 respectively.
A finally took one from 3rd heap. At this time, all the stones were taken away. B had no stones, so B lost.

C. L. Bouton provides a solution for the Nim game:
Consider the number of stones in each heap a1, a2 ,..., An is binary, so the number of Nim in the current game is a1, a2 ,..., The bitwise OR of. For example, in the above example, 3 = 11 (2), 2 = 10 (2), 2 = 10 (2), and then the three numbers are exclusive by bit or get 11 (2) = 3. So 3 is the number of Nim in the current game situation.
Without proof, we can conclude that, if both sides of the game are very smart, when the number of Nim is 0, the current player will be defeated. If the number of Nim is not 0, the current player will win.
Consider the example above. After A takes one of the 2nd piles of stones, the number of stones changes to 3, 1, and 2, and the number of Nim is 0, which makes B fail; after that, A always makes the number of Nim in the case of B 0 after each stone acquisition, so A finally won.
Now that you know how to determine whether the current Nim game is successful, complete a slightly more complex task:
Given the current situation of the Nim game, if the game wins, find out how many stones the game player needs to take to defeat the opponent. If there are multiple ways to take stones, please give the least stones. In the above example, in the initial stage, A can take three stones from 1st or one stone from 2nd or three to ensure B is defeated, but because the latter has the least number of stones, the answer is 1 in this case.

Input

The input contains multiple groups of data.
The first behavior n (1 ≤ n ≤ 106) of each group of data indicates the number of stones.
The second row contains n non-negative integers, indicating the number of stones in each heap. The number of stones in each heap cannot exceed 109. Note: You can pile up empty stones.
The input ends with n = 0. Do not process this data.

Output

Output a row of data in each group, which is the minimum number of stones to be taken away. If the current situation is defeated, output-1.

Sample Input
110217 1733 2 241 2 3 40
Sample Output
10-114

Since this question conforms to the rules of the NLE game, it is still simple to write. For details, see my other article on game theory. I will not elaborate on it here.
Note that the operation order of the exclusive or operation is after addition, subtraction, multiplication, and division !!!!!
#include "stdio.h"const int N=1000001;int a[N];int main(){    int i,t,n,min,m;    while(scanf("%d",&n)!=EOF,n)    {        min=100000001;        m=0;        for(i=0;i<n;i++)        {            scanf("%d",&a[i]);            m=m^a[i];        }        if(m==0)        printf("-1\n");        else        {            for(i=0;i<n;i++)            {                t=a[i]-(m^a[i]);                if(min>t&&t>0)                min=t;            }             printf("%d\n",min);        }    }    return 0;}

 

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.