← All coding problems
GCD and LCM
easyPlacement Coding+10 XPTime limit 2s
For each pair of numbers, print their GCD (greatest common divisor) and LCM (least common multiple).
Input:
- line 1:
q - next
qlines: two integersa b
Output: q lines, each gcd lcm.
Constraints: 1 ≤ q ≤ 10⁴, 1 ≤ a, b ≤ 10⁹ (the LCM can reach 10¹⁸ — use 64-bit integers).
Think about it: Euclid's algorithm: gcd(a, b) = gcd(b, a % b), and gcd(a, 0) = a. Then lcm = a / gcd * b — divide before multiplying so the intermediate value doesn't overflow.
Sample input 1
2 12 18 7 5
Sample output 1
6 36 1 35
Sample input 2
1 6 6
Sample output 2
6 6
Read input from standard input and print the answer to standard output. Your code runs in a secure sandbox against the samples (Run) and hidden tests (Submit).