Revenge of GCD
Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 2140 Accepted Submission (s): 596
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 Input32 3 12 3 28 16 3
Sample output1-12
Source Bestcoder Round #10 was in a pit. The GCD parameter is passed int: It is better to find out the greatest common divisor and then greatest common divisor the k factor.
#include <cstdio>#include<cstring>#include<algorithm>#include<math.h>#include<queue>#include<iostream>using namespaceStd;typedefLong LongLL; ll GCD (ll A,ll b) {returnb==0? A:GCD (b,a%b);} LL p[10005]; LL CMP (ll A,ll b) {returnA>b;}intMain () {inttcase; scanf ("%d",&tcase); while(tcase--) {LL a,b,k; scanf ("%lld%lld%lld",&a,&b,&k); LL D=gcd (A, b); intID =0; for(LL i=1; i*i<=d;i++) {///filter out all factors if(d%i==0){ if(I*i==d) p[id++]=i; Else{P[id++]=i; P[id++]=d/i; } } } if(id<k) {printf ("-1\n"); } Else{sort (p,p+id,cmp); printf ("%lld\n", p[k-1]); } } return 0;}
HDU 5019 (number of major K conventions)