您的位置:首页 > 其它

ZCMU-1431-Epic Game

2017-01-19 20:35 369 查看

1431: Epic Game

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 44  Solved: 34

[Submit][Status][Web
Board]

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

HINT

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. Letgcd(a, b) represent
the operation of calculating the greatest common divisor of numbers a and b.
Specifically, gcd(x, 0) = gcd(0, x) = x.

[align=left]In the first sample the game will go like that:[/align]

·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.
[align=left]
[/align]
[align=left]【解析】[/align]
这道题其实就是两个人来玩游戏,看了下面的HINT部分大家应该就会明白了,每次移动的石头就是最大公约数刚开始先n,k求最大公约数,之后就是k减去移动的石头,然后再m和k求最大公约数。第一个人胜利的就打印0,第二个人就打印1。
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int gcd(int a,int b)
{
int temp;
while(b!=0)
{
temp=a%b;
a=b;
b=temp;
}
return a;
}
int main()
{
int n,m,k,p,q;
while(~scanf("%d%d%d",&n,&m,&k))
{
p=0;
while(k>=0)
{
if(p!=0)
{
q=gcd(m,k);
}
else
{
q=gcd(n,k);
}
k=k-q;
p=!p;
}
printf("%d\n",p);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: