POJ2348 Euclid's Game

Original title link: http://poj.org/problem?id=2348

Euclid’s Game

Description

Two 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.

Input

The input consists of a number of lines. Each line contains two positive integers giving the starting two numbers of the game. Stan always starts.

Output

For 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 should not be processed.

Sample Input

34 12
15 24
0 0

Sample Output

Stan wins
Ollie wins

answer

We can understand this process as a multi-stage N i m , there are many piles of stones, each pile of stones has a number of times that can be taken, at least once at a time, can be taken multiple times, only after the previous pile can be taken down, take the first set of data in the sample For example:

(34,12) can take two 12, after taking it, it becomes (12,10) (12,10) can
only be taken once, it becomes (10,2)
(10,2) takes 5 times and becomes ( 2,0)

then transform into a multi-stage N i m The game is 3 piles of stones: 2, 1, 5

in multiple stages N i m , if you can win by taking a pile first, then you can take exactly one stone left from the pile when you take this pair, forcing your opponent to finish the pile; otherwise, you can take the pile of stones directly. When finished, let your opponent go and take down the pile. Only when a certain pile of stones can only be taken once, the first mover has no choice but to take it all.

Then the problem becomes simple, as long as whoever has the first move (that is, the first encounter with a desirable number of times greater than 1), whoever can win.

code
#include<cstdio>
#include<algorithm>
using namespace std;
int n,m;
bool check(int x,int y,bool p)
{
    if(x%y==0)return p;
    if(x/y>1)return p;
    return check(y,x%y,!p);
}
void ac()
{
    if(n<m)swap(n,m);
    if(check(n,m,1))printf("Stan wins\n");
    else printf("Ollie wins\n");
}
int main()
{
    while(scanf("%d%d",&n,&m)&&n&&m)ac();
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324737909&siteId=291194637