Euclid's game
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 1880 accepted submission (s): 825
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 Input
34 1215 240 0
Sample output
Stan winsOllie wins
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.
Code:
/*************************************** * ******** Author: rabbitcreated time: 2014/7/4 13: 47: 33 file name: 3. CPP *************************************** * ********/# pragma comment (linker, "/Stack: 102400000,102400000") # include <stdio. h> # include <iostream> # include <algorithm> # include <sstream> # include <stdlib. h> # include <string. h> # include <limits. h> # include <string> # include <time. h> # include <math. h> # In Clude <queue> # include <stack> # include <set> # include <map> using namespace STD; # define INF 0x3f3f3f3f # define EPS 1e-8 # define PI ACOs (-1.0) typedef long ll; int gcd (int A, int B) {if (a <B) // find out who swap (a, B); If (B = 0) // to terminate a request, return 0; if (a> 2 * B) // return 1; return gcd (B, A % B) ^ 1; // opposite to the win-or-lose condition after obtaining the result} int main () {// freopen ("data. in "," r ", stdin); // freopen (" data. out "," W ", stdout); int A, B; while (CIN> A> B ){ If (! A &&! B) break; int ans = gcd (a, B); // cout <ans <Endl; If (ANS) puts ("Stan wins "); else puts ("Ollie wins");} return 0 ;}