hdu 5019(第K大公约数)

Revenge of GCD

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2140    Accepted Submission(s): 596

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

被坑惨了。。GCD的参数传的INT。。求出最大公约数然后求最大公约数的第K大因子就好。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<math.h>
#include<queue>
#include<iostream>
using namespace std;
typedef long long LL;
LL gcd(LL a,LL b){
    return b==0?a:gcd(b,a%b);
}
LL p[10005];
LL cmp(LL a,LL b){
    return a>b;
}
int main()
{
    int tcase;
    scanf("%d",&tcase);
    while(tcase--){
        LL a,b,k;
        scanf("%lld%lld%lld",&a,&b,&k);
        LL d = gcd(a,b);
        int id = 0;
        for(LL i=1;i*i<=d;i++){ ///筛选出所有因子
            if(d%i==0){
                 if(i*i==d) p[id++]=i;
                 else{
                    p[id++]=i;
                    p[id++]=d/i;
                 }
            }
        }
        if(id<k){
            printf("-1\n");
        }
        else{
            sort(p,p+id,cmp);
            printf("%lld\n",p[k-1]);
        }
    }
    return 0;
}
时间: 2024-10-13 03:01:06

hdu 5019(第K大公约数)的相关文章

XTUOJ ABK(求出A和B的第K大公约数)

Accepted : 21   Submit : 171 Time Limit : 1000 MS   Memory Limit : 65536 KB  题目描述 ABK是一个比A+B还要简单的题目,给出两个整数A,B,求出A和B的第K大公约数. 输入 第一行是一个整数N(N ≤ 10000),表示样例的个数. 以后每行一个样例,为3个整数A,B,K (1≤A,B≤109 , 1≤K≤10) 输出 每行输出一个整数d,表示A,B的第K大公约数 若没有第K大的公约数则输出-1. 思路:先求出a和b

第k大公约数(简单数学,逻辑转换)

遇到一个挺有意思的题目,要求两个数的第k大公约数(当然k=1时就是最大公约数),如 12 6 2 3 范围,a和b<=1e14,k<=1e9. 所以暴力是肯定不行的,这题的关键就是:能被最大公约数整除的一定也是两数的公约数!!这就可以做出来了 1 #include <iostream> 2 #include <string> 3 #include <algorithm> 4 #include <vector> 5 #include <cst

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

D是10^k的约数快速求解整除性问题

[D是10^k的约数快速求解整除性问题] 定理: 证明: 例一: 例二: 例三:

题目556-最大公约数-nyoj20140812

#include <stdio.h>int main(){    int m,n;    while(scanf("%d,%d",&m,&n)!=EOF)    {        int i;        for(i=m;i>0;i--)        if(m%i==0&&n%i==0)        break;        printf("%d\n",i);        }        return 0

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 个数就是答案了......以为可以顺利通过,原来数据量还是非常大滴!!! 正确

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 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

ACM数论之旅3---最大公约数gcd和最小公倍数lcm(苦海无边,回头是岸( ̄? ̄))

gcd(a, b),就是求a和b的最大公约数 lcm(a, b),就是求a和b的最小公倍数 然后有个公式 a*b = gcd * lcm     ( gcd就是gcd(a, b), ( •?∀•? ) 简写你懂吗) 解释(不想看就跳过){ 首先,求一个gcd,然后... a / gcd 和 b / gcd 这两个数互质了,也就是 gcd(   a / gcd ,b / gcd  )  =  1,然后... lcm = gcd *  (a / gcd) * (b / gcd) lcm = (a *