HDU 5019 (number of major K conventions)

Source: Internet
Author: User
Tags greatest common divisor

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.