POJ 2348 Euclid Game (模拟题)

Euclid‘s Game

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7942   Accepted: 3227

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

可以参考:《基础训练题解》哈尔滨出版社 俞经善...等编
题意讲解:两个人Ollie和Stan玩一个游戏,每次输入两个非负数。 Stan为先手,Ollie为后手。每次从大数中减去小数的任意倍数,将其减完后的结果赋值给被减的大数。Ollie刚才的游戏规则,然后进行循环。直到小数不能再大数中拿到一个非0的数为止。最后一个拿到数的玩家为赢家。

算法讲解:大数除以小数,当值大于1时,正在进行此次游戏的玩家为赢家。否则要继续游戏。当游戏进行了偶数次的时候,就是Stan赢,否则就是Ollie赢了。
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int main()
{
	int x, y;
	while(scanf("%d %d", &x, &y)!=EOF)
	{
		if(x==0 && y==0) break;

		int dd;
		int cnt=0; //记录游戏进行次数
		int n=x; int m=y;
		while(n!=0 && m!=0 )
		{
			if(m>n){
				cnt++;
				dd = m/n;
				m=m%n; //大数被赋值成那个差值
				if(dd >= 2) break;
			}
			else{
				cnt++;
				dd=n/m;
				n=n%m;
				if(dd>=2 ) break;
			}
		}
		if(cnt%2==1) printf("Stan wins\n");
		else
			printf("Ollie wins\n");
	}
	return 0;
}

时间: 2024-08-11 01:12:38

POJ 2348 Euclid Game (模拟题)的相关文章

POJ - 2348 Euclid&#39;s Game(博弈论入门题)

题目链接:poj.org/problem?id=2348 题意:给出两个数,两个人进行博弈,每个人都采取最优策略. 每个人可以进行操作:两个数中较大数减去较小数的倍数(可以是1,2...X倍),使得其中一个数先为零的获胜. 每次都先把较小值给a,较大值给b.一开始把必胜态给先手的那个人,然后进行判断. 1.b-a<=a  没办法只能一次一次计算,必胜态不断变换,直到其中一个数刚好为0. 2.b-a>a   不管怎样都是必胜态.b - xa <= a如果这个是必败态,那么b - (x-1)

POJ 2348 Euclid&#39;s Game(博弈)

题目地址:POJ 2348 每一步只有如下三种情况:(假设a>=b) 1:a%b==0    这时候自然是必败态. 2:a<2*b  这时候的下一步是别无选择的,只能是变为(a-b,a),由于该步是唯一的,所以必胜态与必败态是交替的. 3:a>2*b  这时候是必胜态.为什么呢?因为此时总可以转移到一个必败态.由于第2情况的时候两种状态是交替的,而这时候由总可以转换成(a,a%b)和(a,a%b+b),而(a,a%b+b)与(a,a%b)又属于第2种情况的相邻的,所以必有一个是必败态.根

POJ 2348 Euclid&#39;s Game 组合游戏

题目大意:有两个人玩游戏,有两堆石子,每次一个人要从其中一堆石子中拿走一些石子,当出现有一对石子变成0的时候这个人就输了,另一个人就赢了.给出初始石子有多少,问谁能赢. 思路:基础的组合游戏的判定问题,这个题没有给数据范围,非常的坑爹,据说需要long long. 第一次做组合游戏的题目,想想还有些小激动呢.昨天听同学讲了讲,我来现学现卖一下: 由于组合游戏的公平性,那么:如果一个状态的子状态会使先手必败,那么当前状态的先手必胜: 如果不存在一个子状态会使先手必败,那么当前状态先手必败. 利用这

POJ 2348 Euclid&#39;s Game(博弈)题解

题意:有a,b两个数字,两人轮流操作,每次可以选择两个之中较小的数字,然后另一个数字减去选择数字的任意倍数(不能减到负数),直到其中一个为0,不能操作为败 思路:这题用博弈NP思想,必败点和必胜点之间的转化. 我们假设当前状态为(x,y)其中x>=y,那么必有以下任一状态: 1. x < 2 * y:那么这是只能执行一个操作x - y,如果这个操作之后变成必败态,那么当前为必胜态:反之亦然. 2. x >= 2 * y:显然此时有多种选择,假设x = k * y,如果x - (k - 1

POJ 2348 Euclid&#39;s Game

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

POJ 2348 Euclid&#39;s Game(辗转相除博弈+自由度分析)

题目链接:http://poj.org/problem?id=2348 题目大意:给你两个数a,b,Stan和Ollie轮流操作,每次可以将较大的数减去较小的数的整数倍,相减后结果不能小于0,谁先将其中一个数字变成0谁就获胜. 解题思路:看了挑战程序设计上的,这里我们先假设a<b,当b是a的整数倍是必胜态.我们讨论以下b不是a的整数倍,此时a,b的关系按照自由度的观点(第一次听说),可以分为以下两种情况: ①b-a<a ②b-a>a 那么对于①,玩家只有从b中减去a这一个选择.如果b-a

poj 1008:Maya Calendar(模拟题,玛雅日历转换)

Maya Calendar Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 64795   Accepted: 19978 Description During his last sabbatical, professor M. A. Ya made a surprising discovery about the old Maya calendar. From an old knotted message, profes

【POJ】2348 Euclid&#39;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

poj 3087 Shuffle&#39;m Up(模拟题)

Shuffle'm Up Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6143   Accepted: 2880 Description A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed by starting with two stacks of