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. Network(poj 3694)

    题意:一个无向图可以有重边,下面q个操作,每次在两个点间连接一条有向边,每次连接后整个无向图还剩下多少桥(注意是要考虑之前连了的边,每次回答是在上一次的基础之上) /* tarjan+LCA 先用ta ...

  2. android中SQLite实现

    SQLite操作类: package com.example.administrator.myapplication; import android.content.Context; import a ...

  3. [bzoj1430]小猴打架_prufer序列

    小猴打架 bzoj-1430 题目大意:题目链接. 注释:略. 想法: 我们发现打架的情况就是一棵树. 我们只需要把确定树的形态然后乘以$(n-1)!$表示生成这棵树时边的顺序. 一共$n$个节点我们 ...

  4. MongoDB集群搭建教程收集(待实践)

    先收集,后续再实践. MongoDB的集群应该和MySQL的定位保持一致,因为要认为它就是一个数据库. 集群方式有也是有很多,比如分库,分片,主从,主主等等. 下面是收集的一些教程: http://b ...

  5. How to remote debug neutron

    First of all, I will assume that you know how to use pydevd to remote debug normal python program. I ...

  6. scala 入门Eclipse环境搭建及第一个入门经典程序HelloWorld

    scala 入门Eclipse环境搭建及第一个入门经典程序HelloWorld 学习了: http://blog.csdn.net/wangmuming/article/details/3407911 ...

  7. 实战恢复2950交换机的IOS

    本来想用两台交换机做实验的,可是通过console口进入其中一台交换机后却发现这个台交换器的IOS文件丢失了 本来正常进入交换机后应该是首先进入到用户模式的,而且提示符应该是">&qu ...

  8. java 代理模式具体解释

    java 动态代理(JDK和cglib) 设计模式这东东每次看到就明确可过段时间又不能非常流利的说出来.今天就用具体的比喻和实例来加深自己的理解(小弟水平不高有不正确的地方希望大家能指出来). (1) ...

  9. SDUT--找朋友(BFS&amp;&amp;DFS)

    找朋友 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描写叙述 X,作为户外运动的忠实爱好者,总是不想呆在家里.如今,他想把死宅Y从家 ...

  10. 软考之J2SE

    特别感谢软考让我如今就接触了神奇的java.曾经尽管真不知道java是个神马,看完马士兵的视频发现里面的东西并不陌生.有vb,c++,c#做基础加上这次的J2SE发现原来编程语言有非常多同样的特性.也 ...