Hangzhou Electric (HDU) 5019 Revenge of GCD

Source: Internet
Author: User
Tags greatest common divisor

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

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.