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 <= T <= 100
- 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
求第K大GCD;
那么我们枚举GCD 的因子就好;
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
#include<cctype>
//#pragma GCC optimize("O3")
using namespace std;
#define maxn 200005
#define inf 0x3f3f3f3f
#define INF 0x7fffffff
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const int mod = 10000007;
#define Mod 20100403
#define sq(x) (x)*(x)
#define eps 1e-10
const int N = 1505;
inline int rd() {
int x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
}
ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; }
int T;
ll a, b;
int k;
vector<ll>vc;
bool cmp(ll a, ll b) {
return a > b;
}
int main()
{
//ios::sync_with_stdio(false);
rdint(T);
while (T--) {
rdllt(a); rdllt(b);
rdint(k);
vc.clear();
ll Gcd = gcd(a, b);
for (int i = 1; i <= sqrt(Gcd); i++) {
if (Gcd%i == 0) {
if (i*i != Gcd) {
vc.push_back(i); vc.push_back(Gcd / i);
}
else {
vc.push_back(i);
}
}
}
sort(vc.begin(), vc.end(), cmp);
// cout << vc.size() << endl;
// for (int i = 0; i < vc.size(); i++) {
// cout << vc[i] << ' ';
// }
// cout << endl;
if (k > vc.size()) {
cout << -1 << endl;
}
else {
cout << vc[k - 1] << endl;
}
}
}