Revenge of GCDTime
limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 1724 Accepted Submission (s): 472
Problem DescriptionIn Mathematics, the greatest common divisor (GCD), also known as the greatest common factor (GCF), high EST common factor (HCF), or greatest common measure (GCM), of or more integers (when at least one of the them is not zero) , is the largest positive integer that divides the numbers without a remainder.
---Wikipedia
Today, GCD takes revenge on you. You had to figure out the k-th GCD of X and Y.
Inputthe first line contains a single integer T, indicating the number of test cases.
Each test case is only contains three integers X, Y and K.
[Technical specification]
1.1 <= T <= 100
2.1 <= X, Y, K <= 1 000 000 000 000
Outputfor each test case, output the k-th GCD of X and Y. If No such integer exists, output-1.
Sample Input
32 3 12 3 28 16 3
Sample Output
1-12
The main idea: To find out the two numbers of the K-large approximate. The thought of the topic: I do the idea is to find out the subject of the greatest common divisor, and then find out the greatest common divisor all factors, with the STL set container storage. Using a reverse iterator, you can find the factor K large. The code is as follows:
#include <iostream> #include <string> #include <cmath> #include <cstring> #include <set > typedef __int64 ll; using namespace Std;Set<ll>S;ll GCD(ll xx,ll yy){LL R=1; while (R!=0) {R=Xx%Yy;Xx=Yy;Yy=R; } return Xx;} int Main () { int T;ll X,Y,K,gcd,Count;Cin>>T; while (T--) {Count=0;Cin>>X>>Y>>K;gcd=GCD(X,Y); for (int I=1;I<=sqrt(gcd);I + +) { if( gcd%I==0) {S.Insert(I);S.Insert(gcd/I); } }Set<ll: :Reverse_iterator RIT; for (Rit=S.Rbegin();Rit!=S.Rend();Rit++) {//cout<<*rit<< "";Count++; if (Count==K) {cout<<*Rit<<Endl; Break ; }} if( Count<K)cout<<"-1"<<Endl;S.Clear(); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. Readers are welcome to comment and correct
Hangzhou Electric (HDU) 5019 Revenge of GCD