Two Divisors (Codeforces 1916B)
Tags: Math, number theory

A certain number $$$1 \le x \le 10^9$$$ is chosen. You are given two integers $$$a$$$ and $$$b$$$, which are the two largest divisors of the number $$$x$$$. At the same time, the condition $$$1 \le a < b < x$$$ is satisfied.For the given numbers $$$a$$$, $$$b$$$, you need to find the value of $$$x$$$.$$$^{\dagger}$$$ The number $$$y$$$ is a divisor of the number $$$x$$$ if there is an integer $$$k$$$ such that $$$x = y \cdot k$$$.

Input: Each test consists of several test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then follows the description of the test cases.The only line of each test cases contains two integers $$$a$$$, $$$b$$$ ($$$1 \le a < b \le 10^9$$$).It is guaranteed that $$$a$$$, $$$b$$$ are the two largest divisors for some number $$$1 \le x \le 10^9$$$.

Output: For each test case, output the number $$$x$$$, such that $$$a$$$ and $$$b$$$ are the two largest divisors of the number $$$x$$$.If there are several answers, print any of them.

Note: For the first test case, all divisors less than $$$6$$$ are equal to $$$[1, 2, 3]$$$, among them the two largest will be $$$2$$$ and $$$3$$$.For the third test case, all divisors less than $$$33$$$ are equal to $$$[1, 3, 11]$$$, among them the two largest will be $$$3$$$ and $$$11$$$.For the fifth test case, all divisors less than $$$20$$$ are equal to $$$[1, 2, 4, 5, 10]$$$, among them the two largest will be $$$5$$$ and $$$10$$$.For the sixth test case, all divisors less than $$$12$$$ are equal to $$$[1, 2, 3, 4, 6]$$$, among them the two largest will be $$$4$$$ and $$$6$$$.

Sample Inputs:
8
2 3
1 2
3 11
1 5
5 10
4 6
3 9
250000000 500000000

Sample Outputs:
6
4
33
25
20
12
27
1000000000

Solution (Python):
import sys

def get_ints():
    return map(int, sys.stdin.readline().strip().split())

def get_list():
    return list(map(int, sys.stdin.readline().strip().split()))

def get_list_string():
    return list(map(str, sys.stdin.readline().strip().split()))

def get_string():
    return sys.stdin.readline().strip()

def get_int():
    return int(sys.stdin.readline().strip())

def get_print_int(x):
    sys.stdout.write(str(x) + '\n')

def get_print(x):
    sys.stdout.write(x + '\n')

def get_print_int_same(x):
    sys.stdout.write(str(x) + ' ')

def get_print_same(x):
    sys.stdout.write(x + ' ')
from sys import maxsize
from math import sqrt, floor, ceil, lcm, gcd
from functools import cmp_to_key

def solve():
    for _ in range(get_int()):
        (a, b) = get_ints()
        if a == 1:
            get_print_int(b * b)
        elif b % 2 == 0:
            get_print_int(b * 2)
        else:
            g = gcd(a, b)
            if g == 1:
                get_print_int(lcm(a, b))
            elif g == a:
                get_print_int(b * (b // g))
            else:
                get_print_int(lcm(a, b))
solve()

Solution (Python):
import sys
input = sys.stdin.readline
from math import gcd, isqrt, ceil, log2
t = 1
t = int(input())
for _ in range(t):
    (a, b) = map(int, input().split())
    if b % a == 0:
        print(b * b // a)
    else:
        print(b * a // gcd(a, b))

Solution (Python):
import math
for s in [*open(0)][1:]:
    (a, b) = map(int, s.split())
    print(math.lcm(a, b) * (b // a, 1)[b % a > 0])