ZCMU—1431
2016-12-09 11:20
204 查看
1431: Epic Game
Time Limit: 1 Sec Memory Limit: 128 MB[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 andAntisimon 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 91 1 100
Sample Output
01
HINT
The greatest common divisor of two non-negative integers a and b issuch 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.
[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.
【分析】
本来想找规律...后来发现数据又小..欧几里得算法的速度又强...直接模拟就行了就算数据范围扩大到10W也没问题...
【代码】
#include <stdio.h> int gcd(int x,int y) { return y==0?x:gcd(y,x%y); } int main() { int a,b,c; while (~scanf("%d%d%d",&a,&b,&c)) { int now=0; while (c>=0) { int x; if (now) x=gcd(b,c); else x=gcd(a,c); c-=x; now++; now%=2; } printf("%d\n",now); } return 0; }