您的位置:首页 > 其它

POJ 2348 (博弈)

2016-03-07 14:49 621 查看
Euclid's Game

Time Limit: 1000MS Memory Limit: 65536K
Total 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 12
15 24
0 0

Sample Output
Stan wins
Ollie 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: