HDU 1525 Euclid’s Game

来源:互联网 发布:wear软件安装不了 编辑:程序博客网 时间:2024/06/05 03:23

Problem 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

Source
University of Waterloo Local Contest 2002.09.28

Recommend
LL | We have carefully selected several similar problems for you: 1536 1524 1404 1517 1527

题意:给你两个正整数,较大的数可以减去较小的数的整数倍,谁先拿到0谁胜利。
如果a%b==0,那么先手肯定赢了,
那么先手的人就要创造这种条件。
比方说,他肯定知道必败态,他就故意留个a%b+b的结果给对手。
自然创造这种条件需要满足a>=2*b;

界于 b

#include<iostream>#include<algorithm>#include<cstring>using namespace std;int main(){    int m, n; int ans = -1;    while (cin >> m >> n&&(m + n))    {        int ans = -1;        int x, y; int temp;        x = min(m, n);        y = max(m, n);        while (x != 0)        {            if (y >= x * 2 || y%x==0)            {                break;            }            y = y - x;            temp = x;            x = y;            y = temp;            ans *= -1;        }        if (ans == 1)            cout << "Ollie wins" << endl;        else            cout << "Stan wins" << endl;    }    return 0;}
原创粉丝点击