数学题,第几大的公因子肯定是最大公因子的因子,因此从大到小只要找到最大共因数的第k个因子,那么就是解。
/* * this code is made by LinMeiChen * Problem: * Type of Problem: * Thinking: * Feeling: */ #include<iostream> #include<algorithm> #include<stdlib.h> #include<string.h> #include<stdio.h> #include<math.h> #include<string> #include<vector> #include<queue> #include<list> using namespace std; typedef __int64 lld; typedef unsigned int ud; #define oo 0x3f3f3f3f vector<lld>ar; lld gcd(lld a, lld b) { return b == 0 ? a : gcd(b, a%b); } int main() { int T; lld a, b, k; scanf("%d", &T); while (T--) { scanf("%I64d %I64d %I64d", &a, &b, &k); lld GCD = gcd(a, b); ar.clear(); for (lld i = (int)sqrt((double)GCD); i >= 1; i--) { if (GCD%i == 0) { ar.push_back(i); if (i*i != GCD) ar.push_back(GCD / i); } } sort(ar.begin(), ar.end()); if (ar.size() < k) puts("-1"); else printf("%I64d\n", ar[ar.size() - k]); } return 0; }