uva 11388 GCD LCM(简单数学)

来源:互联网 发布:php推送消息到app 编辑:程序博客网 时间:2024/05/22 17:23

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 TT denotes the number of cases. Each of the next Tlines 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 ba ≤ 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 and will be less than 231.

 

Sample Input

Output for Sample Input

2

1 2

3 4

1 2

-1

 

Problem setter: Shamim Hafiz


题意:给你两个数G,L,让你求两个数a,b,使得gcd(a,b)=G,Lcm(a,b)=L;如果有多个答案输出a最小的,没有则输出-1;L=a*b/G=G*a1*b1;其中gcd(a1,b1)=1;令a1b1=L/G;然后枚举a1b1的约数,符合条件的即为答案;

代码:

#include <iostream>#include<stdio.h>#include<string.h>#include<stdlib.h>#include<vector>#include<stdlib.h>#include<string>#include<map>#include<math.h>#include<queue>#include<stack>#include<set>#define inf 0x3f3f3f3f#define eps 1e-5#define max(a,b) (a)>(b)?(a):(b)#define min(a,b) (a)<(b)?(a):(b)using namespace std;int gcd(int a,int b){    if(b==0)        return a;    return gcd(b,a%b);}int main(){    int t,g,i,l;    while(~scanf("%d",&t))    {        while(t--)        {            scanf("%d%d",&g,&l);            if(l%g!=0)            {                printf("-1\n");                continue;            }            else            {                int ab=l/g;                int k=sqrt(ab+0.0);                bool flag=false;                for(i=1;i<=k;i++)                {                    if(ab%i==0)                    {                        if(i<=ab/i)                        {                            if(gcd(ab/i,i)==1)                            {                                flag=true;                                printf("%d %d\n",i*g,ab/i*g);                                break;                            }                        }                    }                }                if(!flag)                    printf("-1\n");            }        }    }    return 0;}



 

0 0