Description

The GCD of two positive integers is the largest integer that divides both the integers without any remainder. The LCM of two positive integers is the smallest positive integer that is divisible by both the integers. A positive integer can be the GCD of many pairs of numbers. Similarly, it can be the LCM of many pairs of numbers. In this problem, you will be given two positive integers. You have to output a pair of numbers whose GCD is the first number and LCM is the second number.

Input

The first line of input will consist of a positive integer T. T denotes the number of cases. Each of the next T lines will contain two positive integer, G and L.

Output

For each case of input, there will be one line of output. It will contain two positive integers a and b, a ≤ b, which has a GCD of G and LCM of L. In case there is more than one pair satisfying the condition, output the pair for which a is minimized. In case there is no such pair, output ‘-1’.
Constraints
• T ≤ 100
• Both G and L will be less than 2^31

Sample Input

2
1 2
3 4

Sample Output

1 2
-1

解题思路:题目的意思就是给出某两个数的最大公因数G和最小公倍数L,求出这两个数中一个最小和一个最大。推导一下可知:两个数中最小的那个数至少为这两个数的最大公约数,则最大的数为这两个数的最小公倍数。假设这两个数为a,b,则G*L=a*b,因为G最小,而a*b的值一定,即L最大,所以原来的两个数中最小的数为G,最大的数为L。

AC代码:

 #include<bits/stdc++.h>
using namespace std;
int main(){
int t,g,l;
cin>>t;
while(t--){
cin>>g>>l;
if(l%g!=)cout<<-<<endl;
else cout<<g<<' '<<l<<endl;
}
return ;
}

C - GCD LCM的更多相关文章

  1. Mathematics:GCD & LCM Inverse(POJ 2429)

    根据最大公约数和最小公倍数求原来的两个数 题目大意,不翻译了,就是上面链接的意思. 具体思路就是要根据数论来,设a和b的GCD(最大公约数)和LCM(最小公倍数),则a/GCD*b/GCD=LCM/G ...

  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. [POJ 2429] GCD & LCM Inverse

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

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

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

  5. UVA - 11388 GCD LCM

    II U C   ONLINE   C ON TEST  Problem D: GCD LCM Input: standard input Output: standard output The GC ...

  6. hdu-3071 Gcd & Lcm game---质因数分解+状态压缩+线段树

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3071 题目大意: 给定一个长度为n的序列m次操作,操作的种类一共有三种 查询 L :查询一个区间的所 ...

  7. [ 9.13 ]CF每日一题系列—— 340A GCD & LCM

    Description: [ 着实比较羞愧,都想着去暴力,把算法(方法)也忘了] A只涂x,2x,3x……,B只涂y,2y,3y……问你A和B共同涂的墙的个数 Solution: 就是求x和y的lcm ...

  8. 【HDU 5382】 GCD?LCM! (数论、积性函数)

    GCD?LCM! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total ...

  9. 数论入门2——gcd,lcm,exGCD,欧拉定理,乘法逆元,(ex)CRT,(ex)BSGS,(ex)Lucas,原根,Miller-Rabin,Pollard-Rho

    数论入门2 另一种类型的数论... GCD,LCM 定义\(gcd(a,b)\)为a和b的最大公约数,\(lcm(a,b)\)为a和b的最小公倍数,则有: 将a和b分解质因数为\(a=p1^{a1}p ...

  10. 数论3——gcd&&lcm

    gcd(a, b),就是求a和b的最大公约数 lcm(a, b),就是求a和b的最小公倍数 然后有个公式 a*b = gcd * lcm     ( gcd就是gcd(a, b), ( •̀∀•́ ) ...

随机推荐

  1. [NOIP2005] 提高组 洛谷P1054 等价表达式

    题目描述 明明进了中学之后,学到了代数表达式.有一天,他碰到一个很麻烦的选择题.这个题目的题干中首先给出了一个代数表达式,然后列出了若干选项,每个选项也是一个代数表达式,题目的要求是判断选项中哪些代数 ...

  2. Codeforces 22E(图论)

    题意: 给出n个节点,以及和这个节点指向的节点fi,表示从i能够到达fi,问至少需要添加多少条边能够使得原图变为强连通分量, 输出边数及添加的边,多解输出任意一组解. 2 <= n <= ...

  3. spring boot 学习-创建方式

    spring boot是什么 spring boot 是一个快速开发框架,适合小白快速上手开发,它集成了很多优秀的和常用的第三方框架,它简化了xml配置,完全采用注解方式,内部集成了Tomcat.Je ...

  4. Ubuntu 16.04安装TeamViewer

    安装i386库: sudo apt-get install libc6:i386 libgcc1:i386 libasound2:i386 libexpat1:i386 libfontconfig1: ...

  5. Windows 2008 R2 SP1部署WSUS 3.0 SP2

    1 实验环境 1)域: 域名为fengxja.com: 网段:192.168.0网段,不连接外网. 域功能级别和林功能级别为Windows server 2003模式. 2)DC服务器: 域控制器: ...

  6. CentOS firewall添加开放端口

    添加 firewall-cmd --zone=public --add-port=80/tcp --permanent (–permanent永久生效,没有此参数重启后失效) 重新载入 firewal ...

  7. 使用shell分页读取600万+的MySQL数据脚本

    shell-mysql 脚本背景 因为要在Linux上.远程读取mysql的表的数据,然后做一定清洗后.把数据上传至Hadoop集群中,使用Java写吧,感觉太麻烦了.得在Win上开发好,还得打成ja ...

  8. java之Map源代码浅析

    Map是键值对.也是经常使用的数据结构. Map接口定义了map的基本行为.包含最核心的get和put操作,此接口的定义的方法见下图: JDK中有不同的的map实现,分别适用于不同的应用场景.如线程安 ...

  9. 一个python自动化测试的例子

    http://blog.csdn.net/galen2016/article/details/70882483 https://www.cnblogs.com/TankXiao/category/47 ...

  10. 常用Lunix命令

    计算机 1.硬件系统 输入单元.输出单元.算术逻辑单元.控制单元.记忆单元 中央处理单元:CPU(算术逻辑单元.控制单元) 电源.主板.CPU.内存(RAM).硬盘.(声卡.显卡.网卡)(集成在主板上 ...