Euclid's game
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 2074 accepted submission (s): 924
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 12 15 240 0 sample outputstan winsollie wins if the relationship between A and B is a multiple, then the first winner assumes a <B. After a series of operations, (A, B) it will surely become (a, B-B/a * A) // B/A is divisible. If B/a = 1, there will be no initiative, because you can only choose to lose one A. If B/A> 1, you can take the initiative: You can choose to lose B/A * a, so that the people in the future can face (, b-B/A * A), you can also lose (B/A-1) * A, the people behind it must lose a, let yourself face (, b-B/A * A); so as long as you know who has the initiative first, who can win (If yes, B/A> 1)
1 # include <iostream> 2 # include <string> 3 # include <cstdio> 4 # include <vector> 5 # include <queue> 6 # include <stack> 7 # include <algorithm> 8 # include <cstring> 9 # include <stdlib. h> 10 using namespace STD; 11 int P [101000], CNT; 12 INT main () {13 int n, m; 14 While (CIN> N> m, N + M) {15 if (n> m) Swap (n, m); 16 if (M % N = 0) {17 cout <"Stan wins" <Endl; continue; 18} 19 CNT = 0; 20 while (M % n! = 0) {21 p [CNT ++] = M/N; 22 m-= M/N * n; 23 if (n> m) Swap (n, m ); 24} 25 int can = 0, PS =-1; 26 for (INT I = 0; I <CNT; I ++) 27 if (P [I]> 1) {28 PS = I; break; 29} 30 if (PS =-1) {31 if (CNT % 2) cout <"Ollie wins" <Endl; 32 else cout <"Stan wins" <Endl; 33} 34 else {35 if (PS % 2) cout <"Ollie wins" <Endl; // The second Master Initiative 36 else cout <"Stan wins" <Endl; 37} 38} 39}
HDU 1525 Euclid's game