Codeforces Beta Round #90 A题

来源:互联网 发布:英语四级听力技巧知乎 编辑:程序博客网 时间:2024/06/01 07:54
这道题目我们不用看题目什么意思就可以做了,只要看这里:
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.

    这里很清楚的解释了我们要干嘛!先写一个gcd函数,然后循环下去,一直遇到Antisimon should take gcd(5, 0) = 5 stones from the heap. As 0 < 5,

     it is impossible and Antisimon loses.

    就知道结果了,代码如下:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    int gcd(int a,int b)
    {
        int temp,c,d;
        
        while(b)
        {
           d=a%b;
           a=b;
           b=d;
        }
        return a;
    }
    int main()
    {
        int n,m,p,sum;
        scanf("%d%d%d",&n,&m,&p);
        while(1)
        {
           sum=gcd(n,p);
           if(sum>p){printf("1\n") ;break;}
           p-=sum;
           sum=gcd(m,p);
           if(sum>p){printf("0\n");break;}
           p-=sum;
        }
        return 0;
    }