Hdu 2177 (2 heap) Stone Game (Wythoff Game ))

Source: Internet
Author: User

Hdu 2177 (2 heap) Stone Game (Wythoff Game ))

Rock game (2 heaps)

Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission (s): 1649 Accepted Submission (s): 993

Problem Description has two piles of stones, which can be any number and can be different. The game started with taking stones in turn by two people. The game stipulates that there are two different methods to get each time. One is to remove any number of stones from any pile; the other is to take the same number of stones from both piles. Finally, the winner of all the stones. Now we will give you the initial number of two stones. If it is your turn to take them first, let us assume that both Parties adopt the best strategy and ask whether you are the winner or the loser at last. If you win, how do you get a child for 1st times?

The Input contains several rows, indicating the initial conditions of several stones. Each row contains two non-negative integers a and B, indicating the number of two stones. Neither a nor B is greater than 1,000,000, and a <= B. A = B = 0 exit.

The Output also has several rows. If you are the loser at last, the Output value is 0. Otherwise, the Output value is 1, and output the number of the remaining two piles of stones x, y, x <= y after you win 1st times. If you take the same number of stones away from any pile, you can win the same number of stones at the same time in both piles. First, you can output the situation of taking the same number of stones away.

Sample Input
1 2 5 84 72 20 0

Sample Output
014 73 5010 01 2

Author Zhousc
Source ecjtu2008 Summer Contest
Recommend lcy | We have carefully selected several similar problems for you: 2176 2180 1536 1850

There are two piles of stones, the two take turns to take stones, there are two ways to take stones, 1. Take the same number of stones from the two piles at the same time, 2. Just take any number of stones from the heap, And the other heap remains unchanged until the inaccessible ones are manually lost.

Solution:

One of the following major game types is defined as follows:

Wythoff Game: There are several items in two stacks. Two people take the same number of items from one or both stacks in turn, and at least one item is required each time, there are no limits on the number of winners.

Comparison shows that God is similar.

List the following situations:

K :( a, B)

0 :( 0, 0)

1 :( 1, 2)

2 :( 3,5)

3 :( 4, 7)

4: (6, 10)

...

It can be seen that a0 = b0 = 0, ak is the minimum natural number that has not appeared before, and bk = ak + k, the singular situation has the following three properties:

1. Any natural number is contained in one and only one singular situation.
Because ak is the minimum natural number that has not appeared before, so there is ak> ak-1, and bk = ak + k> ak
-1 + k-1 = bk-1> ak-1. So the property 1. Yes.
2. Any operation can change a singular situation to a non-singular situation.
In fact, if only one component of the singular situation (ak, bk) is changed, the other component cannot
In a strange situation, it must be a non-singular situation. If the two components of (ak, bk) are reduced at the same time
The difference remains unchanged, and cannot be the difference of other singular situations. Therefore, it is also a non-singular situation.
3. Use appropriate methods to change a non-singular situation into a singular situation

How can we determine whether we are facing a strange situation? The following formula is used: ak = [k (1 + √ 5)/2], bk = ak + k (k =, 2 ,..., N square brackets indicate the entire function)

For details, see the code.
# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
Using namespace std; int main () {int a, B; while (scanf ("% d", & a, & B), a | B) {if (a> B) swap (a, B); if (a = 0) // whatever the value of B, it must win. Because all the stones of B can be taken at one time, the other party is faced with a strange situation {printf ("1 \ n0 0 \ n"); continue;} int k = B-; // The difference value indicates the nth singular situation int aa = k * (1.0 + sqrt (5.0)/2; // obtain the value of this singular situation based on the formula if (aa = a) // The value of if ("0 \ n"); continue ;} printf ("1 \ n"); // in other cases, int bb = aa + k must be won; // aa and bb are singular situations, that is, the remaining conditions of stones. If (a-aa = B-bb & a> aa) // determine whether the number of stones can be obtained in the same way. Make sure that the other party faces a strange situation, printf ("% d \ n", aa, bb ); k = 2 * a/(1 + sqrt (5.0) + 1; // only take B, a unchanged, according to the value of a, get the value of k using the formula, k is the number of bb = a + k; // obtain the B value of the singular situation according to the number k and a aa = k * (1.0 + sqrt (5.0)/2; // obtain a from the singular situation based on the number k to verify whether a is a value of a from the singular situation if (aa = a & B> bb) printf ("% d \ n", a, bb); k = 2 * a/(3 + sqrt (5.0) + 1; // only take B, a remains unchanged. Based on the value of a, the formula is used to obtain the value of k. k is the number of k !! Different from the position of a above, a is regarded as a singular situation, and now a is regarded as B of a singular situation, and all others are the same as bb =; aa = k * (1.0 + sqrt (5.0)/2; if (bb-k = aa) printf ("% d \ n", aa, ); k = 2 * B/(3 + sqrt (5.0) + 1; // only values a and B remain unchanged. The k value is obtained using the formula of B, k is the number of bb = B; aa = k * (1.0 + sqrt (5.0)/2; if (bb-k = aa & aa <= a &! = B) printf ("% d \ n", aa, B);} return 0 ;}
    
   
  
 

 

 

Related Article

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.