Epic Game(欧几里得算法)

Epic Game
CodeForces - 119A

Simon and Antisimon play a game. Initially each player receives one fixed positive integer that doesn’t change throughout the game. Simon receives number a and Antisimon receives number b. They also have a heap of n stones. The players take turns to make a move and Simon starts. During a move a player should take from the heap the number of stones equal to the greatest common divisor of the fixed number he has received and the number of stones left in the heap. A player loses when he cannot take the required number of stones (i. e. the heap has strictly less stones left than one needs to take).
Your task is to determine by the given a, b and n who wins the game.

Input

The only string contains space-separated integers a, b and n (1 ≤ a, b, n ≤ 100) — the fixed numbers Simon and Antisimon have received correspondingly and the initial number of stones in the pile.

Output

If Simon wins, print “0” (without the quotes), otherwise print “1” (without the quotes).

Examples

Input
3 5 9
Output
0
Input
1 1 100
Output
1

Note

The greatest common divisor of two non-negative integers a and b is such maximum positive integer k, that a is divisible by k without remainder and similarly, b is divisible by k without remainder. Let gcd(a, b) represent the operation of calculating the greatest common divisor of numbers a and b. Specifically, gcd(x, 0) = gcd(0, x) = x.
In the first sample the game will go like that:
Simon should take gcd(3, 9) = 3 stones from the heap. After his move the heap has 6 stones left.
Antisimon should take gcd(5, 6) = 1 stone from the heap. After his move the heap has 5 stones left.
Simon should take gcd(3, 5) = 1 stone from the heap. After his move the heap has 4 stones left.
Antisimon should take gcd(5, 4) = 1 stone from the heap. After his move the heap has 3 stones left.
Simon should take gcd(3, 3) = 3 stones from the heap. After his move the heap has 0 stones left.
Antisimon should take gcd(5, 0) = 5 stones from the heap. As 0 < 5, it is impossible and Antisimon loses.
In the second sample each player during each move takes one stone from the heap. As n is even, Antisimon takes the last stone and Simon can’t make a move after that.

问题简述:
Simon和Antisimom每人拿一个数字,前面有一堆有n个石头的石堆,两人每次从石堆里拿去与自己数字跟石头总数最大公约数相同的石子数,最后谁不能拿相应的石头数就算输(就是公约数大于石头总数了),要求第一行输入三个数字,分别代表Simon、Antisimon、石头总数,输出一行,如果是Simon赢输出“0”,否则输出“1”,表示Antisimon赢。
问题分析:
关键求最大公约数,所以用到了欧几里得算法,算法模板如下:

int gcd(int a, int b)
{
	int temp, k;
	if (b > a)//确保大数除以小数
	{
		temp = a;
		a = b;
		b = temp;
	}
	while ((a%b) != 0)//循环部分,不断大数除以小数求余,最后实现最大公约数
	{
		k = a % b;
		a = b;
		b = k;
	}
	return b;
}

程序说明:
每求得一次最大公约数检验一次石子总数,看是否小于0.
程序实现:

#include <iostream>
using namespace std;
int gcd(int a, int b)
{
	int temp, k;
	if (b > a)
	{
		temp = a;
		a = b;
		b = temp;
	}
	while ((a%b) != 0)
	{
		k = a % b;
		a = b;
		b = k;
	}
	return b;
}
int main()
{
	int a, b, n, c;
	cin >> a >> b >> n;
	while (1)
	{
		c = gcd(a, n);
		n -= c;
		if (n < 0)
		{
			cout << "1" << endl;
			break;
		}
		else if (n == 0)
		{
			cout << "0" << endl;
			break;
		}
		c = gcd(b, n);
		n -= c;
		if (n < 0)
		{
			cout << "0" << endl;
			break;
		}
		else if (n == 0)
		{
			cout << "1" << endl;
			break;
		}
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值