Moderate Modular Mode (Codeforces 1604D)
Tags: Math, number theory

YouKn0wWho has two even integers $$$x$$$ and $$$y$$$. Help him to find an integer $$$n$$$ such that $$$1 \le n \le 2 \cdot 10^{18}$$$ and $$$n \bmod x = y \bmod n$$$. Here, $$$a \bmod b$$$ denotes the remainder of $$$a$$$ after division by $$$b$$$. If there are multiple such integers, output any. It can be shown that such an integer always exists under the given constraints.

Input: The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^5$$$)  — the number of test cases.The first and only line of each test case contains two integers $$$x$$$ and $$$y$$$ ($$$2 \le x, y \le 10^9$$$, both are even).

Output: For each test case, print a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^{18}$$$) that satisfies the condition mentioned in the statement. If there are multiple such integers, output any. It can be shown that such an integer always exists under the given constraints.

Note: In the first test case, $$$4 \bmod 4 = 8 \bmod 4 = 0$$$.In the second test case, $$$10 \bmod 4 = 2 \bmod 10 = 2$$$.In the third test case, $$$420 \bmod 420 = 420 \bmod 420 = 0$$$.

Sample Inputs:
4
4 8
4 2
420 420
69420 42068

Sample Outputs:
4
10
420
9969128

Solution (Python):
import sys
from os import path
if path.exists('input.txt'):
    sys.stdout = open('output.txt', 'w')
    sys.stdin = open('input.txt', 'r')

def main():
    inp = [int(x) for x in sys.stdin.buffer.read().split()]
    ii = 0
    out = []
    t = int(inp[ii])
    ii += 1
    for _ in range(t):
        (x, y) = inp[ii:ii + 2]
        ii += 2
        if x > y:
            n = x + y
        else:
            n = y - y % x // 2
        out.append(n)
        pass
    out_str = '\n'.join((str(x) for x in out))
    sys.stdout.write(out_str)
main()

Solution (Python):
import sys
input = sys.stdin.readline
t = int(input())
ans = []
for _ in range(t):
    [x, y] = [int(_) for _ in input().split()]
    if x == y:
        ans.append(str(x))
    elif x > y:
        ans.append(str(x + y))
    else:
        diff = y % x >> 1
        ans.append(str(y - diff))
sys.stdout.write('\n'.join(ans))

Solution (Python):
t = int(input())
ans = []
for _ in range(t):
    [x, y] = [int(_) for _ in input().split()]
    if x == y:
        ans.append(str(x))
    elif x > y:
        ans.append(str(x + y))
    else:
        diff = y % x >> 1
        ans.append(str(y - diff))
print('\n'.join(ans))