Euclid's game
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 1116 accepted submission (s): 516
Problem descriptiontwo players, Stan and Ollie, play, starting with two natural numbers. stan, the first player, subtracts any positive multiple of the lesser of the two numbers from the greater of the two numbers, provided that the resulting number must be nonnegative. then Ollie, the second player, does the same with the two resulting numbers, then Stan, Etc ., alternately, until one player is able to subtract a multiple of the lesser number from the greater to reach 0, and thereby wins. for example, the players may start with (25, 7 ):
25 7
11 7
4 7
4 3
1 3
1 0
An Stan wins.
Inputthe input consists of a number of lines. Each line contains two positive integers giving the starting two numbers of the game. Stan always starts.
Outputfor each line of input, output one line saying either Stan wins or Ollie wins assuming that both of them play perfectly. The last line of input contains two zeroes and shocould not be processed.
Sample input34 1215 240 0
Sample outputstan winsollie wins
Sourceuniversity of Waterloo local contest 2002.09.28
Recommendll
Question link:Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1525
Question: two positive numbers are given.
Two people take turns to perform the operation:
A large number is a multiple of a small number, and two non-negative values are obtained.
Who first wins the game by reducing one of the numbers to 0. Ask who can win. Stan is a pioneer.
Assume that two numbers are a and B (A> = B)
If a = B, it must be the first hand to win. One step can be reduced to 0, B
If a % B = 0. That is, a is a multiple of B, it is also the first hand to win.
If a> = 2 * B, then the person must know whether a % B and B are both winning or losing. If it is a bid, change A and B to a % B, B first, then the first hand will certainly win. If it is a winning state, first hand to A, B to a % B + B, B. Then the opponent only has to change these two numbers to a % B, B, first hand to win.
If it is B <A <2 * B, there is only one path: A-B, B (at this time, 0 <a-B <B ). in this way, we will continue to see who will face the above winning state first.
/* * If A = B or a> = 2 * B, the first-hand win ratio * If B <A <2 * B. then there is only one path, which is a-B and B. * at this time, the swap size is changed to B and A-B. * In this way, we will keep repeating to see who is facing a = B | A> = 2 * B. */ # Include <Iostream> # Include < String . H> # Include <Algorithm> # Include <Stdio. h> Using Namespace STD; Int Main (){ Int A, B; While (Scanf ( " % D " , & A, & B) = 2 ){ If (A = 0 | B = 0 ) Break ; If (A < B) Swap (A, B ); Bool Flag = True ; While (1 ){ If (A = B | A/B> = 2 ) Break ; =- B; swap (a, B); flag =! Flag ;} If (FLAG) printf ( " Stan wins \ n " ); Else Printf (" Ollie wins \ n " );} Return 0 ;}