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

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

题意:给出你最大公约数和最小公倍数,让你求出原来的两个数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 &amp; LCM Inverse 【java】+【数学】的更多相关文章

  1. [POJ 2429] GCD & LCM Inverse

    GCD & LCM Inverse Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10621   Accepted: ...

  2. POJ 2429 GCD & LCM Inverse (Pollard rho整数分解+dfs枚举)

    题意:给出a和b的gcd和lcm,让你求a和b.按升序输出a和b.若有多组满足条件的a和b,那么输出a+b最小的.思路:lcm=a*b/gcd   lcm/gcd=a/gcd*b/gcd 可知a/gc ...

  3. 1266: gcd和lcm(Java)

    WUSTOJ 1266: gcd和lcm 参考 1naive1的博客 Description   已知a,b的最大公约数为x,也即gcd(a,b)=x; a,b的最小公倍数为y,也即lcm(a,b)= ...

  4. POJ 2429 GCD & LCM Inverse(Pollard_Rho+dfs)

    [题目链接] http://poj.org/problem?id=2429 [题目大意] 给出最大公约数和最小公倍数,满足要求的x和y,且x+y最小 [题解] 我们发现,(x/gcd)*(y/gcd) ...

  5. POJ 2429 GCD & LCM Inverse(Miller-Rabbin素性测试,Pollard rho质因子分解)

    x = lcm/gcd,假设答案为a,b,那么a*b = x且gcd(a,b) = 1,因为均值不等式所以当a越接近sqrt(x),a+b越小. x的范围是int64的,所以要用Pollard_rho ...

  6. POJ:2429-GCD & LCM Inverse(素数判断神题)(Millar-Rabin素性判断和Pollard-rho因子分解)

    原题链接:http://poj.org/problem?id=2429 GCD & LCM Inverse Time Limit: 2000MS Memory Limit: 65536K To ...

  7. POJ2429 GCD & LCM Inverse pollard_rho大整数分解

    Given two positive integers a and b, we can easily calculate the greatest common divisor (GCD) and t ...

  8. hdu 4497 GCD and LCM 数学

    GCD and LCM Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4 ...

  9. POJ2429 - GCD & LCM Inverse(Miller–Rabin+Pollard's rho)

    题目大意 给定两个数a,b的GCD和LCM,要求你求出a+b最小的a,b 题解 GCD(a,b)=G GCD(a/G,b/G)=1 LCM(a/G,b/G)=a/G*b/G=a*b/G^2=L/G 这 ...

随机推荐

  1. RabbitMQ集群下队列存放消息的问题

    RabbitMQ中队列有两种模式 1.默认 Default 2.镜像 Mirror [类似于mongoDB,从一直在通过主的操作日志来进行同步] *如果将队列定义为镜像模式,那么这个队列也将区分主从, ...

  2. 配置Eclipse编写HTML/JS/CSS/JSP页面的自动提示

    我们平时用eclipse开发jsp页面时智能提示效果不太理想,今天用了两个小时发现了eclipse也可以像Visual Studio 2008那样完全智能提示HTML/JS/CSS代码,使用eclip ...

  3. python 双层函数调用顺序

    读大神代码,见到大神封装的接口很多都是采用双层函数形式. def 外层函数(外层参数) def 内层函数(内层参数) 函数体 return 值 return 内层函数 类似这样的形式,使用 外层函数( ...

  4. Ubuntu下mysql使用

    1. 从网上安装 sudo apt-get install mysql-server.装完已经自动配置好环境变量,可以直接使用mysql的命令. 注:建议将/etc/apt/source.list中的 ...

  5. SqlServer中Sql查看存储过程

    ( 一)利用Sql语句查询数据库中的所有表 1.利用sysobjects系统表 select * from sysobjects where xtype='U'  2,利用sys.tables目录视图 ...

  6. Python 驱动 MongoDB 示例(PyMongo)

    Python 的MongoDB驱动 pymongo ,使用pip Install pymongo安装即可 最近发现网上的很多实例已经过时了,在此自我探究记录下来. 编写一个接口类来支持MongoDB的 ...

  7. 2.选择元素 - 自定义过滤器《jquery实战》

    2.5.6 自定义过滤器 jQuery 中有两种方法创建自定义的过滤器.第一种比较简单,但是不鼓励,从 jQuery 1.8 开始已经被第二种方法取代.记住,使用新方法时,你自定义的过滤器在 jQue ...

  8. 《Redis设计与实现》学习笔记

    第2章 简单动态字符串(SDS) redis的字符串不是直接用c语言的字符串,而是用了一种称为简单动态字符串(SDS)的抽象类型,并将其作为默认字符串. redis中包含字符串值的键值对在底层都是由S ...

  9. mavean项目的jar位置的影响

    由于项目的数据库需求改变了,有mysql数据库变为oracle的,那么对于项目就是需要改变数据库连接池.这个项目运用了mavean框架,那么下载jar在pom.xml文件中填写就可以了,但是oracl ...

  10. tidb 升级步骤

    1.检查ansible版本,正常情况下,2.1 rc3需要兼容ansible 2.5以上的版本 $ ansible --version 2.检查python两个模块jinja2需要升级到2.9.6或以 ...