HDU 5019 Revenge of GCD(数学)

题目链接: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

BestCoder Round #10

官方题解:

代码如下:

#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;
}
时间: 2024-08-08 05:37:05

HDU 5019 Revenge of GCD(数学)的相关文章

HDU 5019 Revenge of GCD

Revenge of GCD Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 430    Accepted Submission(s): 122 Problem Description In mathematics, the greatest common divisor (gcd), also known as the greate

hdu 5019 Revenge of GCD(BestCoder Round #10)

Revenge of GCD                                                              Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 668    Accepted Submission(s): 195 Problem Description In mathematics

杭电(hdu)5019 Revenge of GCD

Revenge of GCD Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1724    Accepted Submission(s): 472 Problem Description In mathematics, the greatest common divisor (gcd), also known as the great

HDOJ 5019 Revenge of GCD

第k大GCD = GCD/第K大因子 Revenge of GCD Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 877    Accepted Submission(s): 259 Problem Description In mathematics, the greatest common divisor (gcd), also

HDU ACM 5019 Revenge of GCD

分析:只需要求出最大公约数,然后枚举最大公约数的因子,把他们保存起来在求第K大的:因为是最大公约数的因子必然是两个数的因子.另外循环变量i和个数cnt都要声明为__int64,否则出错. #include<iostream> #include<algorithm> using namespace std; __int64 gcd(__int64 x,__int64 y) { __int64 r; while(y) { r=x%y; x=y; y=r; } return x; } _

HDU 1019 Least Common Multiple 数学题解

求一组数据的最小公倍数. 先求公约数在求公倍数,利用公倍数,连续求所有数的公倍数就可以了. #include <stdio.h> int GCD(int a, int b) { return b? GCD(b, a%b) : a; } inline int LCM(int a, int b) { return a / GCD(a, b) * b; } int main() { int T, m, a, b; scanf("%d", &T); while (T--)

hdu 1577 WisKey的眼神 (数学几何)

WisKey的眼神 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2059    Accepted Submission(s): 625 Problem Description WisKey的眼镜有500多度,所以眼神不大好,而且他有个习惯,就是走路喜欢看着地(不是为了拣钱哦^_^),所以大家下次碰见他的时候最好主动打下招呼,呵呵.但是

HDU 4910 Problem about GCD

Problem about GCD Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 470    Accepted Submission(s): 77 Problem Description Given integer m. Find multiplication of all 1<=a<=m such gcd(a, m)=1 (cop

HDU 4983 Goffi and GCD(数论)

HDU 4983 Goffi and GCD 思路:数论题,如果k为2和n为1,那么只可能1种,其他的k > 2就是0种,那么其实只要考虑k = 1的情况了,k = 1的时候,枚举n的因子,然后等于求该因子满足的个数,那么gcd(x, n) = 该因子的个数为phi(n / 该因子),然后再利用乘法原理计算即可 代码: #include <cstdio> #include <cstring> #include <cmath> typedef long long l