CSDN Heroes-Fifth annual online programming contest Month III: Stone games (1)

Source: Internet
Author: User

Topic details
A B Two people face a number of stones, each of which the number of stones can be arbitrarily determined.
The two men took turns to take some stones according to the following rules, the rules of the game are as follows:
1. Each step should take at least one stone;
2. Each step can only take part or all of the stones from a heap;
3. If anyone fails to take a child according to the rules, who is the loser?
If a B two people take the optimal strategy, a first take, ask, is a win or B win.
Input format:
Multiple sets of data, two rows per set of data, the first line being an integer N, 2<=n<=10000
The next line is n positive integers, which represent the number of stones per heap, and the number of stones is within a 32-bit integer.
Output format:
Each set of test data output line, if a win strategy, output "win", otherwise output "Lost"
Answer Instructions
Input sample
3
3 3 1
Sample output:
Win

This is a wonderful game theory to the problem. (The classic model of game theory:Nim Games )

First, in a recursive way:
1, if there is only a pile of stones, then a can be won (directly all take away), because the game stipulates: A B Two people have to take the best strategy . Then it means that in the case of a B in this premise is to win.

2. If there are two piles of stones, we begin to discuss:
For such a situation, a will fail.
For [n,1] Such a situation, a can be [n,1] to [], so in this case a win.
and for [n,m] such a situation, according to the previous discussion, a need to avoid is: after a fetch can not be [n,1], then if is taken as [n,2] it?
Assuming that we adopt such a strategy, the same [n,2] still belongs to [N,m] class problems. Such a strategy would inevitably result in [2,2] such a situation. And for the face of [2,2] This situation of the player, it will be defeated.
And so on [3,3]. Then it can be concluded that if the opening is [N,n] class, then a will be defeated, conversely, if the opening is, [N,m] class, then a win. (After all, if m>n, then you can [N,m]=>[n,n], then B will fail)

How do we deduce to n heaps?
We can divide each heap into several small piles in the same way. n A large heap, expressed as n k-dimensional vectors, requires that the first and second dimensions of each vector are not related to the J dimension (I not Equal to j) (i.e., for any vector a, there is a[I]=1 and a[j]! =1, (I!=J)), each dimension value type is a Boolean type, expressed as whether the large heap is present in the small heap that is divided on that dimension. Then, determine the number of small piles in each dimension of n k-dimensional vectors, and if they are even, a must fail (reason refers to the previous discussion)
Specifically, for a set of sample 8,11,2,14, these four large piles can be expressed as four four-dimensional vectors, they will be divided into 1 stones for a small heap, two stones for a small pile, four stones for a small heap, eight pebbles for a small pile of four kinds of small heap.
08->[1,0,0,0]
11->[1,0,1,1]
02->[0,0,1,0]
14->[1,1,1,0]
From this we can know that a win.
The example 3,3,1 can be divided into two-dimensional vectors
3->[1,1]
3->[1,1]
1->[0,1]
From this learned, a win.

Because the integer stored in the computer is binary, you can think of the number of stones of type int as a 32-dimensional vector (required in the topic) and for each dimension, you only need to know if it is even, then it can be solved by bitwise operation.
So we need to design a Boolean function that satisfies the requirements:

A B | Ans
——————————
0 0 | 0
0 1 | 1
1 0 | 1
0 0 | 0

So you can push the show. function: ans= (~a & B) | (A & ~b)
Where 0 means that the dimension is initially or calculated as an even number of small heaps, and vice versa, is an odd number of small heaps. As a result of the above division, if the vector indicates the existence of a small heap, then there are only 1 sub-I small heap, that is, odd number of small heap. (This operation is very similar to the addition operation without rounding)
And the end result is to see if all is even, just to determine whether the number in the accumulator is 0

The code is as follows:

 #include <stdio.h>     int  Main () {int  N; while  (scanf  ( "%d" , &n)! = EOF)        {int  s = 0 ; while  (n--)            {int  temp; scanf             ( "%d" , &temp); s = (~s & temp) |        (S & ~temp); } s? printf  ( "win\n" ): printf  (     "lost\n" ); } return  0 ;}  

Csdn Heroes-fifth annual online programming contest Month III: Stone Games (1)

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.