poj 2429 GCD & LCM Inverse 【java】+【数学】
2014-11-17 23:27
337 查看
GCD & LCM Inverse
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
Sample Output
题意:给出你最大公约数和最小公倍数,让你求出原来的两个数a,b;特别的如果有多组的话,输出和最小的哪一组。
分析:由GCD和LCM之间的关系可得a*b/GCD= LCM; 没有特别的方法只好枚举,但是我们可以得出a*b = LCM/GCD;我们要找的是最后的和最小的,所以从sqrt(b/=a)开始到1枚举就好了。
注:如果用c/c++会超时的。最后经学长指导改用java就过了。。。又学了一招。
代码:
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 9928 | Accepted: 1843 |
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
题意:给出你最大公约数和最小公倍数,让你求出原来的两个数a,b;特别的如果有多组的话,输出和最小的哪一组。
分析:由GCD和LCM之间的关系可得a*b/GCD= LCM; 没有特别的方法只好枚举,但是我们可以得出a*b = LCM/GCD;我们要找的是最后的和最小的,所以从sqrt(b/=a)开始到1枚举就好了。
注:如果用c/c++会超时的。最后经学长指导改用java就过了。。。又学了一招。
代码:
import java.util.Scanner; import java.math.*; public class Main{ public static void main(String[] args){ Scanner cin = new Scanner(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】+【数学】
- POJ2429 GCD&LCM Inverse(整数分解,由GCD+LCM求a,b)
- POJ2429 GCD & LCM Inverse (大整数分解)
- POJ2429_GCD & LCM Inverse【Miller Rabin素数测试】【Pollar Rho整数分解】
- POJ 2429 GCD &amp; LCM Inverse(大数分解)
- [POJ 2429] GCD & LCM Inverse
- poj 2429 GCD & LCM Inverse
- POJ2429 - GCD & LCM Inverse(Miller–Rabin+Pollard's rho)
- poj2429 GCD & LCM Inverse 因数分解pollard_rho算法
- poj 2429 GCD & LCM Inverse
- poj 2429 GCD & LCM Inverse(拉宾米勒测试+大数分解+dfs)
- POJ 2429 GCD & LCM Inverse (整数分解,由gcd+lcm求a,b)
- poj2429 GCD & LCM Inverse
- POJ 2429 - GCD & LCM Inverse(数论)
- poj 2429 GCD & LCM Inverse
- POJ 2429 GCD&LCM Inverse [pollard_rho]【数论】
- Poj2429 inverse of LCM&GCD
- POJ 2429 GCD & LCM Inverse (Pollard rho整数分解+dfs枚举)
- POJ 2429 GCD & LCM Inverse(Pollard_Rho+dfs)
- POJ 2429 GCD & LCM Inverse (大整数素性测试与因式分解)