首页 > 其他 > 详细

POJ5429 GCD & LCM Inverse

时间:2014-11-15 15:35:49      阅读:245      评论:0      收藏:0      [点我收藏+]

GCD & LCM Inverse
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 9913   Accepted: 1841

Description

Given two positive integers a and b, we can easily calculate the greatest common divisor (GCD) and the least common multiple (LCM) of a and b. But what about the inverse? That is: given GCD and LCM, finding a and b.

Input

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

Output

For each test case, output a and b in ascending order. If there are multiple solutions, output the pair with smallest a + b.

Sample Input

3 60

Sample Output

12 15

Source

算是钻空子解法了。。算法没变,C++ TLE,java ac..

import java.util.Scanner;

public class Main {
	static long gcd(long a, long b) {
		long c;
		while(b != 0) {
			c = a % b;
			a = b;
			b = c;
		}
		return a;
	}

	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		long a, b, c, i;
		while(cin.hasNext()) {
			a = cin.nextLong();
			b = cin.nextLong();
			c = b / a;
			for(i = (long)Math.sqrt(c); i > 0; --i)
				if(c % i == 0 && gcd(i, c / i) == 1) {
					System.out.println(i*a + " " + c/i*a);
					break;
				}
		}
	}
}


POJ5429 GCD & LCM Inverse

原文:http://blog.csdn.net/chang_mu/article/details/41144967

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!