HDU1525 Euclid's Game 博弈

6 篇文章 0 订阅
4 篇文章 0 订阅

Euclid’s Game

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5444 Accepted Submission(s): 2572

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

问题连接

问题描述

开始有两个数,每一操作只能把两个数中大的数减去小的数的整数倍,并要求结果不为负数,谁先使其中一个数为0谁就赢了。Stan先开始,然后到Ollie,然后再到Stan…问如果两个人都十分聪明,问谁会赢。

问题分析

因为两个人能知道怎么才能让自己不会输,所以从一开始,对他们来说,一方必赢另一方必输。换句话说,如果不按最优的方案,那么才会让局势颠倒,否则一直都是这样的状态。
给出两个数a,b(因为操作与数的位置无关,所在在这规定a>=b)
有几种情况:
1.a|b。谁面对这种情况谁赢,因为可以一步得到(0,b)
2.a>2b。这种情况可以得到两种情况,且由操作者决定。一种是(a%b+b,b),另一种是(a%b,b)。而哪一种对于操作者是必胜态,操作者是清楚的,所以出现这种情况的时候,也是面对者必赢。附:还有一种情况是(a’,b),a’>2b,相当于把选择权给对方,所以肯定不会这么做。
3.a<2*b。也就是a=b+(a%b),情况2的前一个操作结果,如果面对这种情况,该次的操作者只能得到的结果是(a%b,b)对应情况2的后一个操作结果,所以还是说明了出现情况2的时候,局势已经被一方掌控了。
情况3,对于我们来说可能不知道谁胜谁负,但由情况3最终可以到情况1或者情况2。所以我们只需要模拟一下就可以知道谁输谁赢。

代码如下

#include<iostream>
using namespace std;
int main() {
	int a, b, t;
	bool flag;
	while (cin >> a >> b && (a || b)) {
		flag = 1;
		if (a < b) swap(a, b);
		while (1) {
			if (b == 0 || a % b == 0 || a > 2 * b) break;
			//a > b&&a < 2 * b
			t = a % b;
			a = b;
			b = t;
			flag = !flag;
		}
		if (flag) cout << "Stan wins\n";
		else cout << "Ollie wins\n";
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值