hdu-2504-again see GCD (Java Forced violent water too!) )

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): 12371 Accepted Submission (s): 5257


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
Recommendlcy | We have carefully selected several similar problems for you:2501 1003 1106 1257 1124

This question is a bit misleading, I just started to look at the time! The greatest common divisor of a and C are B, and the C minimum is required! That C must be 2*b! The only thing to consider is the time of a==2*b! that C equals 3*b!. Is this not a brain problem? But......    This idea is wrong! Because when you find out what you think of C, it is possible that the a,c greatest common divisor has changed at this point B is not greatest common divisor, because B may not have you find the common factor in A and C! like a=24 b=4 c=20 instead of a=24 b=4 C=16

Give several sets of test data: a=100 b=2 c=6a=8 b=2 c=6a=4 b=4 c=8a=10 b=5 c=15
My Method! Java violence water too! Find all multiples of B, and then GCD algorithm to judge!

Import Java.io.*;import java.util.*;p ublic class main{public static void Main (string[] args) {//TODO auto-generated Metho  D stubscanner input = new Scanner (system.in); int n = Input.nextint (); for (int i=0;i<n;i++) {int a = Input.nextint (); int b = Input.nextint (); for (int. j=2;j<1000000;j++) {if (B==GCD (a,b*j)) {System.out.println (b*j); break;}}} public static int GCD (int a,int c) {if (a<c) {return GCD (c,a);} if (a%c!=0) {return GCD (c, a%c);} Else{return c;}}}



hdu-2504-again see GCD (Java Forced violent water too!) )

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.