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. centos6.5环境DNS-本地DNS服务器bind的搭建

    centos6.5环境DNS-本地DNS服务器bind的搭建 域名系统(英文:Domain Name System,缩写:DNS)是因特网的一项服务.它作为将域名和IP地址相互映射的一个分布式数据库, ...

  2. 使用Python自己实现简单的数据可视化

    只使用Python的random库,将已有数据生成HTML格式的标签云.思路就是根据同一单词出现的次数多少,生成不同大小不同颜色单词的数据的视图. 比如以下格式的多条数据: 1 Gaming 1 Sk ...

  3. JavaEE 学习框架

    JavaSE JavaWeb基础 ssh+hibernate+spring ssm+spring+mybatis 项目1 电商项目(项目二)

  4. NOIp 2018 普及组

    T1标题统计 传送门 题目描述 凯凯刚写了一篇美妙的作文,请问这篇作文的标题中有多少个字符? 注意:标题中可能包含大.小写英文字母.数字字符.空格和换行符.统计标题字 符数时,空格和换行符不计算在内. ...

  5. 【LOJ】#2492. 「BJOI2018」二进制

    题解 每次开这样的数据结构题感想都大概是如下两点 1.为什么别人代码长度都是我的1/2???? 2.为什么我运行时间都是他们的两倍???? 简单分析一下,我们关注一个区间是否合法只关注这个区间有多少个 ...

  6. 迷茫于Hibernate/JPA的人提一些建议。

    想对那些“迷惑”于Java ORM框架的J2EE开发人员提一些建议,希望能够对他们 更深入的理解和运用J2EE ORM框架来提速工作有所帮助,这些建议可能显得有些”陈旧“和”肤浅“, 因为最近半年我没 ...

  7. 工作流调度器azkaban2.5.0的安装和使用

    为什么需要工作流调度系统 一个完整的数据分析系统通常都是由大量任务单元组成: shell脚本程序,java程序,mapreduce程序.hive脚本等 各任务单元之间存在时间先后及前后依赖关系 为了很 ...

  8. python selenium-webdriver 环境搭建(一)

    selenium 虽然过了这么多年,但是到目前为止依然是比较流行的自动化框架了,还有很多的初学者在学习,所以根据自己的时间将把相关的资料汇总一下,下面首先我们需要搭建一下基础环境. 首先自己本身比较笨 ...

  9. ld: -pie can only be used when targeting iOS 4.2 or later

    ld: -pie can only be used when targeting iOS 4.2 or later clang: error: linker command failed with e ...

  10. Win10解决无法访问其他机器共享的问题

    Win10解决无法访问其他机器共享的问题     你不能访问此共享文件夹,因为你组织的安全策略阻止未经身份验证的来宾访问.这些策略可帮助保护你的电脑免受网络上不安全设备或恶意设备的威胁. 管理员身份执 ...