HDU 2054 See also GCD (water problem??)

Source: Internet
Author: User
Tags gcd greatest common divisor

See GCD again.Time limit:1000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 15221 Accepted Submission (s): 6397


Problem description has three positive integers a,b,c (0<a,b,c<10^6), where c is not equal to B. If the greatest common divisor of a and C are B, it is now known A and b to satisfy the minimum c of the condition.

Input the first line to enter an n, indicating that there are n sets of test data, the next n rows, each line entered two positive integers, a, a.

The output outputs correspond to C, one row for each set of test data.

Sample Input
26 212 4

Sample Output
48

Source "ACM Program Design" Short Semester exam _ software engineering and other major
original title link: http://acm.hdu.edu.cn/showproblem.php?pid=2504Analysis:know that the a,c greatest common divisor B, known as A, a, the smallest c is known as GCD (a,c) = B, the smallest C. Do not be naïve to think that c=2b;can not be ignored is that the topic requires b! = C, that is, the C value starts from 2b, and then step by step c and then determine whether the a,c greatest common divisor is equal to B, the output C when satisfied.

AC Code:
#include <iostream>using namespace Std;int GCD (int x,int y) {    return y==0?x:gcd (y,x%y);} int main () {    int t,a,b;    cin>>t;    while (t--)    {        cin>>a>>b;        int c = 2*b;        while (GCD (a,c)!=b)        {            c+=b;        }        cout<<c<<endl;    }    return 0;}


HDU 2054 See also GCD (water problem??)

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.