Hdu 1527 Stone Game

Source: Internet
Author: User
take the stone game Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 3778 accepted Submission (s): 1902


Problem Description There are two piles of stones, the number of arbitrary, can be different. The game begins with two people taking turns to take stones. The game rules, each time there are two different ways, one can be in any one of the pile to take away any number of stones, two can be in both the same number of stones to take the same amount of gravel. Finally, all the stones are taken out by the winner. Now give me the initial number of stones, and if it's your turn to take first, assume that both sides are taking the best strategy and asking if you're the winner or loser.
Input input contains rows that represent the initial condition of several types of stones, each of which contains two nonnegative integers a and B, indicating the number of stones, A and b are not greater than 1,000,000,000.
Output outputs correspond to a number of rows, each containing a number 1 or 0, or 1 if you are the winner, or 0 in the end.
Sample Input
2 1 8 4 4 7
Sample Output
0 1 0/*: Witzov game problem, draw a PN map, you can find the law. Regarding Witzov question, here has the concrete detailed explanation: http://baike.so.com/doc/1561844.html
*/

Witzov game (Wythoff Game): There are two piles of several items, two people take turns from a pile or two piles from the same number of items, the provision of at least one at a time, many are not limited to the final light of the winner.

In this case, it is rather complicated. We use (AK,BK) (AK≤BK, k=0,1,2,...,n) to represent the number of items and call them the situation, and if a face (0,0), then A has lost, this situation we call the singular situation. The first few singular situations are: (0,0), (1,2), (3,5), (4,7), (6,10), (8,13), (9,15), (11,18), (12,20).

Two if all use the correct operation, then the face of the extraordinary situation, the first to win, and vice versa, then the winner.

So let a situation (a,b), how to judge it is not a singular situation. We have the following formula:

AK =[k (1+√5)/2],bk= AK + k (k=0,1,2,...n square brackets denote rounding function)

The wonderful thing is that there is a golden split number (1+√5)/2 = 1.618 ... thus, the rectangle composed of AK,BK is approximately the golden rectangle, and since 2/(1+√5) = (√5-1)/2, the J=[a (√5-1)/2] can be obtained first, if A=[J (1+√5)/2], then a = AJ,BJ = AJ + j, if not equal, then a = Aj+1,bj+1 = aj+1 + j + 1, if not, then it is not a singular situation. And then follow the above rules, you will encounter strange situation.

AC Code:

#include <cstdio>
#include <cmath>
int main ()
{
    int a,b;
    while (scanf ("%d%d", &a,&b)!=eof)
    {
      if (a>b)
      {
        a=a^b;
        B=b^a;
        a=a^b;
      }
      Double t= (1+SQRT (5))/2.0;
      if ((int) (t* (b-a)) ==a)
        printf ("0\n");
      else
        printf ("1\n");
    }
    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.