1.23 B

B - 2

Time limit 2000 ms
Memory limit 262144 kB

Problem Description

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).

Sample Input

3 5 9

1 1 100

Sample Output

0

1

问题链接:B - 2

问题简述:

输入a,b,n,分别代表
甲(Simon)一开始有的石头数
乙(Antisimon)一开始有的石头数、
石头总数
每次要从石头堆里拿 a(或b)和目前石头堆的最大公约数那么多的石头(例如,a是3,现在石头堆有6,那么要拿6和3的最大公约数3那么多石头,石头堆一开始是n,会越拿越少),最后谁不够拿那么多石头的一方输。

问题分析:

我们来看sample input
3 5 9

一开始拿3个(3和9的最大公约数),石头堆剩9-3=6
然后拿1个(5和6的最大公约数),石头堆剩6-1=5
再然后拿1个(3和5的最大公约数),石头堆剩5-1=4
and then拿1个(5和4的最大公约数),石头堆剩4-1=3
然后拿3个(3和3的最大公约数),石头堆剩0个
乙拿不到了就输了

最大公约数可以用欧几里得算法,模板如下:

	int GCD(int a, int b)
	{
		if (b == 0)
		{
			return a;
		}
		else
		{
			return GCD(b, a%b);
		}
	}

程序说明:

注意GCD的使用

AC通过的C语言程序如下:

#include <iostream>
using namespace std;
int GCD(int a, int b)
{
	if (b == 0)
	{
		return a;
	}
	else
	{
		return GCD(b, a%b);
	}
}
int main()
{
	int a, b, n;
	cin >> a >> b >> n;
	while (1)
	{
		n = n - GCD(a, n);
		if (n < 0)
		{
			cout << "1";goto End;
		}
		n = n - GCD(b, n);
		if (n < 0)
		{
			cout << "0";goto End;
		}
	}
End:;
}

猜你喜欢

转载自blog.csdn.net/weixin_44003969/article/details/86611090