uva 11388 GCD LCM(簡單數學)

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;
}



 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章