hdu-5019 Revenge of GCD
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5019
题目大意:
求A和B的第k个gcd
解题思路:
直接求出A和B的gcd,A和B的第k个gcd就是A和B的gcd的第k个因子
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int maxn = 1e5 + 10; 5 ll a[maxn]; 6 int main() 7 { 8 int T; 9 cin >> T; 10 while(T--) 11 { 12 ll x, y, k, tot = 0; 13 scanf("%lld%lld%lld", &x, &y, &k); 14 ll g = __gcd(x, y); 15 for(ll i = 1; i * i <= g; i++) 16 { 17 if(g % i == 0) 18 { 19 a[tot++] = i; 20 if(i * i != g)a[tot++] = g / i; 21 } 22 } 23 if(k > tot)printf("-1\n"); 24 else 25 { 26 sort(a, a + tot); 27 k = tot - k + 1; 28 printf("%lld\n", a[k - 1]); 29 } 30 } 31 return 0; 32 }
越努力,越幸运