HDU 5019 Revenge of GCD

http://acm.hdu.edu.cn/showproblem.php?pid=5019

Revenge of GCD

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


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

 

 1 #include <iostream>
 2 #include <vector>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 long long gcd(long long a, long long b)
 7 {
 8     if (b == 0)
 9         return a;
10     return gcd(b, a%b);
11 }
12 
13 
14 int main() {
15     vector<long long> vec;
16     int T;
17     long long x, y, k;
18     cin >> T;
19 
20     while (T--) {
21         vec.clear();
22         cin >> x >> y >> k;
23         long long mg = gcd(x, y);
24         
25         for (long long  i = 1; i*i <= mg; i++) {
26             if (mg%i == 0) {
27                 vec.push_back(i);
28                 if (i*i != mg)
29                     vec.push_back(mg / i);
30             }
31         }
32 
33         sort(vec.begin(), vec.end());
34         if (vec.size() <k) cout <<-1<< endl;
35         else cout << vec[vec.size() - k] << endl;
36    }
37     return 0;
38 }

 

posted on 2016-11-13 20:07  mkfoy  阅读(247)  评论(0编辑  收藏  举报

导航