POJ2348 Euclid's Game 欧几里得博弈

题源:http://poj.org/problem?id=2348
题意:给你两个数 两个人轮流操作 每次可以这样操作:把较大数减去较小数的k倍(k=1,2,3…),谁先让其中一个变0,谁就赢了。
思路:欧几里得博弈
思路来源:https://blog.csdn.net/just_sort/article/details/61413912
但是自己看了很多网上的题解还是看不明白,于是又借鉴了一下赵鑫大佬share的代码,真的强,用递归操作,特别强!
下面是递归的核心代码:

bool dfs(ll a,ll b){
	if(a<b) {//保证a>=b
		ll temp=a;a=b;b=temp;//最好用分号 不然可能顺序出现问题 
	}
	//若当前这个人拿到手里的时候,已经出现一个为0,那当前这个人就输了
	if(b==0) return false;
	//若当前这个人可以把其中一个变0,那这个人就赢了
	if(a%b==0) return true;
	//若当前这个人可以操控当前状态变为赢的状态或者输的状态,那稳赢!
	if(a>=b*2) return true;
	//不然的话需要看看下一个人是否能赢  递归查询所以不断的负负得正
	if(dfs(a-b,b)) return false;
	return true;
}

网上搜题解还有很多循环写法,恕我驽钝实在是看不懂,所以在此膜一下赵鑫大佬,虽然他铁定看不见,哈哈!
下面附AC代码,是赵鑫大佬的做法。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
#define ll long long
#define inf 0x3f3f3f3f
#define MID (t[k].l+t[k].r)>>1
#define cl(a,b) memset(a,b,sizeof(a))
//欧几里得博弈  这个是赵鑫大佬的做法 tql
using namespace std;
bool dfs(ll a,ll b){
	if(a<b) {
		ll temp=a;a=b;b=temp;//最好用分号 不然可能顺序出现问题 
	}
	if(b==0) return false;
	if(a%b==0) return true;
	if(a>=b*2) return true;
	if(dfs(a-b,b)) return false;
	return true;
}
int main(){
	ll a,b;
	while(scanf("%lld%lld",&a,&b)!=EOF){
		if(a==0&&b==0) break;
		if(dfs(a,b)) printf("Stan wins\n");
		else printf("Ollie wins\n");
	}
	return 0;
}

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值