uva—gcd lcm

来源:互联网 发布:lua编辑器 for mac 编辑:程序博客网 时间:2024/06/07 12:59


IIU  ONLINE   C ON TEST  2 008

Problem D: GCD LCM

Input: standard input
Output: standard output

 

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 integersa and b, a ≤ b, which has a GCD ofG and LCM of L. In case there is more than one pair satisfying the condition, output the pair for whicha is minimized. In case there is no such pair, output -1.

 

Constraints

-           T ≤ 100

-           Both G and L will be less than 231.

 

Sample Input

Output for Sample Input

2

1 2

3 4

1 2

-1

 

Problem setter: Shamim Hafiz

题意:给出了gcd(最大公约数),lcm(最小公倍数),求满足gcd,lcm的两个数n,m,输出最小的一组

解析:设n=a*b*c*d,m=a*b*c*e,那么易知gcd=a*b*c,lcm=a*b*c*d*e;即lcm%gcd==0.

           再逆向思考给出gcd=a*b*c,lcm=a*b*c*d*e.那么有以下几种情况:

           1.n=a*b*c,m=a*b*c*d*e

           2.n=a*b*c*d,m=a*b*c*e

           3.n=a*b*c*e,m=a*b*c*d

           4.n=a*b*c*d*e,m=a*b*c

          即d和e是可以自由排列的,且明显第一组是最小的。

 if(l%g==0)    cout<<g<<" "<<l<<endl; else     cout<<"-1"<<endl;
代码:

#include <iostream>using namespace std;int main(){    int t;    cin>>t;    while(t--)    {        int g,l;        cin>>g>>l;        if(l%g==0)            cout<<g<<" "<<l<<endl;        else            cout<<"-1"<<endl;    }}




0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 驾驶证过期2年半怎么办 审车逾期一个月怎么办 摩托车驾驶证年审过期一个月怎么办 摩托车驾驶证记满12分怎么办 b2驾驶证扣2分该怎么办 b2扣6分以上怎么办 审驾照晚了3天怎么办 考驾驶证3年到期怎么办 学习驾驶证明过期了怎么办 a2扣了12分怎么办 驾照a2扣6分了怎么办 a2本扣9分怎么办 驾驶证分扣3分怎么办? 异地换驾驶证没有居住证怎么办 b2开c1车扣分怎么办 驾照五次没考过怎么办 大车行驶证丢了怎么办 车的产权证丢了怎么办 车子行驶证掉了怎么办 定期的存折丢了怎么办 存折密码输错6次怎么办 营业执照原件丢失怎么办怎么注销 违章扣了14分怎么办 c1驾驶本过期了怎么办 考驾照没带身份证怎么办 上海扣满12分怎么办 美宝旅行证丢失怎么办 汽车证件全丢了怎么办 车的行驶本丢了怎么办 车和行驶证丢了怎么办 考驾照人在外地怎么办 外地考驾照没有居住证怎么办 考驾驶证预约密码忘了怎么办 考驾照密码忘了怎么办 考驾照的密码忘了怎么办 手机银行登录密码忘了怎么办 宽带账号或密码错误怎么办 车险过户联系不上原车主怎么办 换车了etc忘拆了怎么办 c1d驾驶证d证到期了怎么办 摩托车驾驶证过五年怎么办