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 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 <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;

typedef long long int LL;

LL gcd(LL a,LL b)
{
    if(b==0) return a;
    return gcd(b,a%b);
}

LL factor[1000100],n;

int main()
{
    int T_T;
    scanf("%d",&T_T);
    while(T_T--)
    {
        LL x,y,k;
        cin>>x>>y>>k;
        LL g=gcd(x,y);
        LL d=sqrt(g);
        n=0;
        for(LL i=1;i<=d;i++)
        {
            if(g%i==0)
            {
                LL j=g/i;
                factor[n++]=i;
                if(j!=i) factor[n++]=j;
            }
        }
        sort(factor,factor+n);
        if(k>n) puts("-1");
        else
        {
            cout<<g/factor[k-1]<<endl;
        }
    }
    return 0;
}
时间: 2024-12-28 11:31:25

HDOJ 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

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 i

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; } _

hdoj 5087 Revenge of LIS II 【第二长单调递增子】

称号:hdoj 5087 Revenge of LIS II 题意:非常easy,给你一个序列,让你求第二长单调递增子序列. 分析:事实上非常easy.不知道比赛的时候为什么那么多了判掉了. 我们用O(n^2)的时间求单调递增子序列的时候,里面在加一层循环维护sum数组.表示前面有几个能够转移当当前,求前面sum的和保存到当前. 最后求最后一个sum[n-1]是否为1就ok.为1的话在最长的基础上减一,否则就是最长的. AC代码: #include <iostream> #include &l

hdoj 2504 又见GCD 【GCD判定】

思路:一个一个的找,因为c不等于b 且b是(a, c)的最大公约数, 所以c是b的整数倍, 每找到一个c就判断与 a的最大公约数是不是b,不是的话,就继续 刚开始的时候 居然把gcd非递归形式忘了...也没想用递归形式.. 又见GCD Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 10151    Accepted Submissio

hdoj 5087 Revenge of LIS II 【第二长单调递增子序列】

题目:hdoj 5087 Revenge of LIS II 题意:很简单,给你一个序列,让你求第二长单调递增子序列. 分析:其实很简单,不知道比赛的时候为什么那么多了判掉了. 我们用O(n^2)的时间求单调递增子序列的时候,里面在加一层循环维护sum数组,表示前面有几个可以转移当当前,求前面sum的和保存到当前. 最后求最后一个sum[n-1]是否为1就ok,为1的话在最长的基础上减一,否则就是最长的. AC代码: #include <iostream> #include <algor

BestCoder10 1001 Revenge of GCD(hdu 5019) 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5019 题目意思:给出 X 和 Y,求出 第 K 个 X 和 Y 的最大公约数. 例如8 16,它们的公约数依次为1 2 4 8,那么第 3 个 GCD(X, Y) = 2,也就是从后往前数第3个公共因子. TLE思路:求出 X 的所有因子(从小到大开始存储),再从后往前枚举这些因子,检查是否也为 Y 的因子,统计到第 K 个数就是答案了......以为可以顺利通过,原来数据量还是非常大滴!!! 正确