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"