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

求第k大公约数。

求出最大公约数,枚举它的所有因子就行了。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
vector<long long> s;
long long gcd(long long a,long long b)
{
    return b==0?a:gcd(b,a%b);
}
int main()
{
    long long x,y,k;
    int t;
    scanf("%d",&t);
    while(t--)
    {
        s.clear();
        scanf("%I64d%I64d%I64d",&x,&y,&k);
        long long u=gcd(x,y);
        for(long long i=1;i*i<=u;i++)
        {
            if(u%i==0)
            {
              s.push_back(i);
              if(i*i!=u)
              s.push_back(u/i);
            }
        }
        if(s.size()<k)
        {
            printf("-1\n");
        }
        else
        {
            sort(s.begin(),s.end());
            printf("%I64d\n",s[s.size()-k]);
        }
    }
    return 0;
}

时间: 2024-10-13 05:25:52

hdu 5019 Revenge of GCD(BestCoder Round #10)的相关文章

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 4932 Miaomiao&#39;s Geometry(BestCoder Round #4)

Problem Description: There are N point on X-axis . Miaomiao would like to cover them ALL by using segments with same length. There are 2 limits: 1.A point is convered if there is a segments T , the point is the left end or the right end of T.2.The le

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 5086 Revenge of Segment Tree(BestCoder Round #16)

Revenge of Segment Tree                                                          Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 420    Accepted Submission(s): 180 Problem Description In comput

HDU - 5210 - Delete &amp;&amp; 5211 - Mutiple (BestCoder Round #39)

题目传送:HDU - 5210 HDU - 5210 - Delete 思路:简单题 AC代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include <vector> #include &l

HDU 5063 Operation the Sequence(BestCoder Round #13)

Problem Description: You have an array consisting of n integers: a1=1,a2=2,a3=3,…,an=n. Then give you m operators, you should process all the operators in order. Each operator is one of four types:Type1: O 1 call fun1();Type2: O 2 call fun2();Type3:

HDU 4883 TIANKENG’s restaurant(BestCoder Round #2)

Problem Description: TIANKENG manages a restaurant after graduating from ZCMU, and tens of thousands of customers come to have meal because of its delicious dishes. Today n groups of customers come to enjoy their meal, and there are Xi persons in the

hdu 5229 ZCC loves strings(Bestcoder Round #41)

ZCC loves strings                                                       Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/131072 K (Java/Others) Total Submission(s): 286    Accepted Submission(s): 108 Problem Description ZCC has got N st

HDU 5805 - NanoApe Loves Sequence (BestCoder Round #86)

先找相邻差值的最大,第二大,第三大 删去端点会减少一个值, 删去其余点会减少两个值,新增一个值,所以新增和现存的最大的值比较一下取最大即可 1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 using namespace std; 5 #define LL long long 6 const int N = 100005; 7 int t, n, p1, p2, p3; 8 LL a[N]; 9