UVA11388 GCD LCM

( Link point here )

topic:

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

Sample Input 2 1 2 3 4

Sample Output 1 2 -

Subject to the effect:

 

First on the code!

. 1 #include <bits / STDC ++ H.>
 2  the using  namespace STD;
 . 3  int T;
 . 4  Long  Long A, B, C, D, ANS [ 101 ] [ . 3 ];
 . 5  int main ()
 . 6  {
 . 7      CIN >> T ;
 . 8      for ( int I = . 1 ; I <= T; I ++ )
 . 9      {
 10          CIN >> A >> B;
 . 11          IF (A% B == 0 ) // if LCM GCD may be divisible by 
12          {
 13             ans[i][1]=a;
14             ans[i][2]=b;
15             continue;
16         }
17         else ans[i][1]=-1;
18     }
19     for(int i=1;i<=t;i++)
20     if(ans[i][1]==-1)
21     printf("-1\n");
22     else
23     printf("%lld %lld\n",ans[i][1],ans[i][2]);
24     return 0;
25 }

Why can write so it? Now I talk about in detail:

prove:

Let's see if statement on line 11: When the GCD can be divisible by LCM, GCD number two and LCM.

We set up two numbers are a, b. (A <= b)

First, we know that, GCD must be divisible by a and b, and a and b and can be divisible by LCM, GCD it must be divisible by LCM.

And we know that when a number divisible by x y, their GCD is x, LCM is y

And because GCD * k = a (k> = 1 && k == int (k)), so a> = GCD, the minimum value of GCD.

So we are here to set up a minimum. (I.e. a = GCD)

We identified a future, but according to the formula: GCD * LCM = a * b, where GCD, a, LCM is known, the value of b must be fixed, and is equal to LCM.

Therefore, when a minimum value (a = GCD (a, b), b = LCM (a, b)) when, (a, b) meet the requirements of the optimal solution.

QED.

 

After the first 11 line card over, we come to the issuing bank first 17 else statement.

But it is still using the GCD must be divisible by a and b, and a and b and can be divisible by LCM, so GCD LCM must be divisible by this principle.

So be sure no solution.

QED.

This time we finished card. O (t) algorithm.

(In fact, if you want to become O (1) algorithm, only need to enter a, a re-export. But I am not used to it )

If you feel you have a more subtle way, welcome to leave a comment below.

Guess you like

Origin www.cnblogs.com/xinxiyuan/p/11332841.html