Games: Take a stone game (POJ 1067)

Source: Internet
Author: User

Take a stone game
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 37662 Accepted: 12594

Description

There are two piles of stones, the quantity is arbitrary, can be different. The game began with two people taking turns to take stones. The game rules, each time there are two different ways, one can take away any number of stones in any heap, and two can take the same number of stones in both piles. Finally, the stones are all taken out as the winner. Now give the number of the initial two piles of stones, if it's your turn to take it first, assume that both sides adopt the best strategy and ask whether you are the winner or the loser.

Input

The input contains several lines that represent the initial condition of several kinds of stones, each of which contains two nonnegative integers a and B, indicating the number of stones, A and B, not greater than 1,000,000,000.

Output

The output corresponds to a number of lines, each containing a number 1 or 0, or 1 if you are the winner, or 0 for the other. 2015-08-19 This problem is more interesting, the code is not much, but the inside of the mathematical knowledge contains a richer. The problem is famous. Witzov Game Title said, we have to do is to determine who is the final winner, then this problem, take a lot of methods of stone (do not be taken to the best strategy to cheat, not necessarily to take the meaning of), we can not be a one-way enumeration, so too tired, also not desirable, how to do? In fact, in the game of two people, as long as the pieces under the victory of the kind, and meet certain rules, then there must be a party must win the situation, This is actually the knowledge of graph theory, we wait to speak again, not urgent.
We now first observe, since there must be a win situation, then according to test instructions, we are most likely to think of IS (0,k), (k,0), (k,k) These three cases, if there are three cases, we must win, then if we must lose the situation what looks like? Let's think about it, (2,1), (3,5), (5,3) .... This kind of situation we must be lost, do not believe you can try, we take (to) see, no matter which side of the stone you take, or take a kind of, you are lost. In this way, it seems that the problem is really difficult, because not only there is a certain win situation, but also there is a certain loss of the situation, there is not necessarily win or not necessarily lose the situation, that is finished, this question is not able to do it? Don't worry, we think: If you are in the winning state, do not think, you win. The premise of this situation is that the opponent is in a losing state .If you want to ensure that you win, first of all you can not be defeated in the state, and the best you after the opponent must be defeated in the state, this idea can, if you are not in the winning state, you can transfer yourself to the winning state, or the opponent forced to the failure of the statesuch a thought so this problem is good to do, we only use to find will be defeated, the key is how to find it? With this question, let's talk about a very important model: The graph theory model of game---must be defeated and kernel 1
   TH1 : The winning state must be a certain strategy to the inevitable, and the failure to take any strategy will go to the winning state. graph theory Language:
   TH2: because the must-fail state can only go to the winning state, so there is no edge between any of the two must-fail nodes;
TH3: because the winning state can always go to the inevitable, so to any non-mandatory state of the node,     there must be an edge from which it points to a must-fail node.

Definition 1: in the direction graph, the set X is infinite between any two points, which is called the set X as the inner fixation set.
Definition 2: a directed graph, any point that is not in the set X has an edge pointing to the set X, which is called the set X as an outer fixation.
Definition 3: in the direction graph, set X is both an external and an internal fixation set, which is called the set X as the nucleus.
TH4: Double game, agreed to take the last step to win, if there is a nuclear existence, then one of the unbeaten strategy.
TH5: There is a unique nucleus for the non-loop graph of the finite nodes.
Theorem 1 theorem 2 and theorem 3 We do not testify (this according to the figure to understand a bit better), for the theorem 4, because the game must correspond to the direction of the graph, we can assume a to go first, a always do the B force into the core of the situation, and then B can exit the nuclear outside (assuming still exist to the outside finger) There is always a situation where a will eventually push B into the corner (of course, you may ask if B is out of the way after a cornered, that's impossible, because if so, a long time in the nuclear, violating the definition of nuclear).    Similarly, B is the same analysis for Theorem 5: Because the graph has no loop, so there must be an end, the end of the set is the nucleus. Well know the graph theory of this knowledge, back to the topic, then this problem, will be defeated is the nuclear collection, then how to find the nuclear? We can push the idea and find the rules we know(0,0) is a special must-fail point, then according to the non-mandatory points can go to the point of failure, we according to the conditions of the topic, we can use 3 lines to remove the non-mandatory, horizontal, vertical, and slash, then we took out (0,k), (k,0), (k,k) (k>1) point, Next we find the first not zoned point,He must be a loser., because this point cannot and (0,0) have the intersection, conforms to the nuclear definition, we have found (2,1) and (we), we also follow above the processing method, the dash. Next is (3,5), (5,3) These two must defeat point, the same, then is (4,7) (7,4) ... ...... ..... ..... ............ Keep pushing backwards. Now we're going to put all these points together and see, okay, did you find the law? The law is a (k) =b (k) +k, this thing we write code should be familiar with, this equation of a (k) and B (k) ratio, is the golden section rate of 0.618 (of course, K is very big is approaching this number) is not a bit strange? You may say, no ah this is just the law of our drawing, how can we draw conclusions? Well, here's a little proof: proof {(A (k), B (k)) |a (k) =b (k) +k} are all in the core: contradiction: Suppose there is a {(A (k), B (k)) |a (k) =b (k) +k} is not in the nucleus, then this point is bound to exist with a certain nucleus, then the side how to come? Or it's just sideways. Come up, or come diagonally (that is, there is another node (c (N), d (n)) so that its coordinates are equal to C (n) =a (k)-M or D (n) =b (k)-M or C (n) =a (k)-m,d (n) =b (k)-M). Because before considering (c (n), d (n)), we have eliminated all the nodes on these edges and edges, so{(A (k), B (k)) |a (k) =b (k) +k} are all in the nucleus. Proof of completion In addition, this conclusion can lead to a second well-known theorem: beatty theorem 2
Beatty theorem is something, you can see in the following reference, we here only to conclude that isThe Witzov of the game will satisfy the Beatty theorem, and if A (n) =[αn],b (n) =[βn], then α= (sqrt (5)-1)/2;
Then the next step is to show you the code of Witzov game, it is really simple to die:
#include <stdio.h>#include<math.h>intMainvoid){    Long Doublep = (sqrt (Long Double)5) + (Long Double)1) / (Long Double)2; intA, B, K;  while(~SCANF ("%d%d",&a,&b)) {k= b > A? B-a: A-b; A= b > A?a:b; if(A = = (int) (P*k)) printf ("0\n"); Elseprintf"1\n"); }}

Resources:

Copyright notice: When reproduced, please indicate the original source and author information and this statement in the form of hyperlinks.

1.http://blog.sina.com.cn/s/blog_727d57100100ql6e.html (My proof method is to disprove, he that is a mathematical induction, more rigorous point)

2.http://www.cnblogs.com/jackge/archive/2013/04/22/3034968.html

3.http://yjq24.blogbus.com/logs/42653430.html

4.http://yjq24.blogbus.com/logs/42826226.html

5.http://www.java3z.com/cwbwebhome/article/article19/res046.html

Games: Take a stone game (POJ 1067)

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.