Given a and B, this question is to calculate the approximate number of C.
The longest result is the maximum common divisor, that is, the common GCD algorithm. But now we want to ask for a common divisor in section C. we can imagine that if we make the common divisor in section C as X and the maximum common divisor as G, then what about x | G?
We can intuitively understand that the maximum common divisor is actually obtained by multiplying the common prime factor after the prime factor is decomposed by A and B respectively. For any common divisor A and B, it must include the prime factor of the largest common divisor. Therefore, X | G.
Therefore, we only need to enumerate the factors of G for the approximate number of C. We know that we can perform O (SQRT (N) for the factor of a number )) the N range of this question is 10 ^ 12, so the complexity is sufficient.
But for a long time, I haven't written such a question with a very tight time limit. I am a little bit confused about writing more or enumerating some content, so it is really powerless. Or you are too scum.
#include <iostream>#include <algorithm>#include <vector>#include <cstdio>#include <cstdlib>using namespace std;typedef __int64 LL;LL gcd(LL a, LL b){ if(b==0) return a; return gcd(b, a%b);}int main(){ LL a, b, c; int T; vector<LL> v; scanf("%d", &T); while(T--) { scanf("%I64d%I64d%I64d", &a, &b, &c); LL temp = gcd(a, b); v.clear(); int cnt = 0; for(LL i=1; i*i<=temp; ++i) { if(temp%i==0) { v.push_back(i); if(i*i!=temp) v.push_back(temp/i); } } sort(v.begin(), v.end()); if(v.size() >= c) printf("%I64d\n", v[v.size()-c]); else printf("-1\n"); } return 0;}
HDU 5019 simple mathematical questions