GCD LCM

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.c++

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.less

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^31ide

Sample Input

2
1 2
3 4this

Sample Output

1 2
-1spa

解題思路:題目的意思就是給出某兩個數的最大公因數G和最小公倍數L,求出這兩個數中一個最小和一個最大。推導一下可知:兩個數中最小的那個數至少爲這兩個數的最大公約數,則最大的數爲這兩個數的最小公倍數。假設這兩個數爲a,b,則G*L=a*b,由於G最小,而a*b的值必定,即L最大,因此原來的兩個數中最小的數爲G,最大的數爲L。code

AC代碼:blog

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int main(){
 4     int t,g,l;
 5     cin>>t;
 6     while(t--){
 7         cin>>g>>l;
 8         if(l%g!=0)cout<<-1<<endl;
 9         else cout<<g<<' '<<l<<endl;
10     }
11     return 0;
12 }
相關文章
相關標籤/搜索