码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 5019 Revenge of GCD(数学)

时间:2014-09-19 22:32:36      阅读:271      评论:0      收藏:0      [点我收藏+]

标签:数学   hdu   

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


Problem Description
In mathematics, the greatest common divisor (gcd), also known as the greatest common factor (gcf), highest common factor (hcf), or greatest common measure (gcm), of two or more integers (when at least one of them is not zero), is the largest positive integer that divides the numbers without a remainder.
---Wikipedia

Today, GCD takes revenge on you. You have to figure out the k-th GCD of X and Y.
 
Input
The first line contains a single integer T, indicating the number of test cases. 

Each test case only contains three integers X, Y and K.

[Technical Specification]
1. 1 <= T <= 100
2. 1 <= X, Y, K <= 1 000 000 000 000
 
Output
For each test case, output the k-th GCD of X and Y. If no such integer exists, output -1.
 
Sample Input
3 2 3 1 2 3 2 8 16 3
 
Sample Output
1 -1 2
 
Source


官方题解:

bubuko.com,布布扣


代码如下:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
typedef __int64 LL;
vector<LL>v;
LL GCD(LL a, LL b)
{
    if(b == 0)
        return a;
    return GCD(b,a%b);
}

int main()
{
    int t;
    LL x, y, k;
    scanf("%d",&t);
    while(t--)
    {
        v.clear();
        scanf("%I64d%I64d%I64d",&x,&y,&k);
        LL tt = GCD(x,y);
        // printf("%I64d\n",tt);
        for(LL i = 1; i*i <= tt; i++)
        {
            if(tt%i == 0)
            {
                v.push_back(i);
                if(i*i != tt)//防止放入两个i
                    v.push_back(tt/i);
            }
        }
        sort(v.begin(), v.end());
        if(k > v.size())
            printf("-1\n");
        else
            printf("%I64d\n",v[v.size()-k]);
    }
    return 0;
}


HDU 5019 Revenge of GCD(数学)

标签:数学   hdu   

原文地址:http://blog.csdn.net/u012860063/article/details/39403953

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!