ACM学习历程—HDU 5512 Pagodas(数学)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5512

学习菊苣的博客,只粘链接,不粘题目描述了。

题目大意就是给了初始的集合{a, b},然后取集合里的两个元素进行加或者减的操作,生成新的元素。问最后最多能生成多少个元素。问答案的奇偶性。

首先一开始有a, b。那么如果生成了b-a(b>a),自然原来的数同样可以由b-a, a生成(b != 2a)

于是如此反复下去,最后的数必然是可以由两个数p, 2p生成的。

于是所有的数肯定可以表示成xp+y*2p = (x+2y)p (x >= 0 && y >= 0)

很显然,最后集合里面所有的数都是p的倍数。而且p的所有小于等于n的倍数构成的集合就是原题中的集合。

于是只需要判断n/p的奇偶性。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <queue>
#include <vector>
#define LL long long

using namespace std;

int n, a, b;

void turn(int &a, int &b)
{
    while (b)
    {
        if (a > b) swap(a, b);
        b = b%a;
    }
}

int main()
{
    //freopen("test.in", "r", stdin);
    int T;
    scanf("%d", &T);
    for (int times = 0; times < T; ++times)
    {
        scanf("%d%d%d", &n, &a, &b);
        turn(a, b);
        printf("Case #%d: ", times+1);
        if (n/a%2) printf("Yuwgna\n");
        else printf("Iaka\n");
    }
    return 0;
}
View Code

 

posted on 2015-11-07 20:40  AndyQsmart  阅读(658)  评论(0编辑  收藏  举报

导航