POJ 2429 GCD & LCM Inverse "java" + "math"

Source: Internet
Author: User
Tags gcd greatest common divisor

GCD & LCM Inverse
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 9928 Accepted: 1843

Description

Given positive integers A and B, we can easily calculate the greatest common divisor (GCD) and the least common Multip Le (LCM) of A and B. But what's about the inverse? That Is:given GCD and LCM, finding a and B.

Input

The input contains multiple test cases, each of the which contains the positive integers, the GCD and the LCM. You can assume that these the numbers is both less than 2^63.

Output

For each test case, the output A and B in ascending order. If There is multiple solutions, output the pair with smallest A + B.

Sample Input

3 60

Sample Output

12 15
Test instructions: give you greatest common divisor and least common multiple, let you find the original two number A, a, especially if there are more than one group, the output and the smallest group.

Analysis: The relationship between GCD and LCM can be a*b/gcd= LCM; there is no special way to enumerate, but we can draw a*b = LCM/GCD; We're looking for the last and the smallest, so it's good to start with the sqrt (b/=a) to the 1 enumeration.

Note: If you have a time-out in C + +. Finally, after the guidance of seniors to use Java ... and learned a trick.

Code:

Import Java.util.scanner;import java.math.*;p ublic class main{public static void Main (string[] args) {Scanner cin = new SCA Nner (system.in); Long A, B, X, Y;while (Cin.hasnext ()) {a = Cin.nextlong (); b = Cin.nextlong (); x = y = 0;b/= a;for (Long i = ( Long) math.sqrt (b); i > 0; I--) {if (b%i = = 0&&GCD (i, b/i) = = 1) {x = i*a; y = b/i*a; break;}} System.out.println (x+ "" +y);} public static long gcd (long A, long B) {if (a<b) {long T =a; a = b; b = t;} if (b = = 0) return a;else return gcd (b, a%b);}}



POJ 2429 GCD & LCM Inverse "java" + "math"

Related Article

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.