POJ 2348 (博弈)

来源:互联网 发布:淘宝嘉贝逸飞旗舰店 编辑:程序博客网 时间:2024/06/09 14:40
Euclid's Game
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 8502 Accepted: 3451

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 1215 240 0

Sample Output

Stan winsOllie wins

Source

Waterloo local 2002.09.28

题意:给出两个整数,每一次都用大整数减去小整数的倍数,率先出现0的人获胜。

对于当前状态1,不管过程如何两个整数(n, m)(n>=m)都将会变成状态2:(m, n%m),所以如果状态2必败,那么状态1就必胜;否则的话考虑n最多减去几次m,如果n/m的结果取整大于1,那么先手就可以使状态变成(n%m+m, m)从而必胜,否则必败。

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;bool win (int n, int m) {    if (m == 0)        return 0;    if (!win (m, n%m))        return 1;    int t = n/m;    if (t > 1)        return 1;    return 0;}int main () {    int n, m;    while (scanf ("%d%d", &n, &m) == 2) {        if (!n && !m)            break;        if (n < m)            swap (n ,m);        printf ("%s\n", win (n, m) ? "Stan wins" : "Ollie wins");    }    return 0;}


0 0