Chef has two nephews who love integers XX and YY. An integer NN is awesome if the following conditions hold:
- 1≤N≤1018
- (N+Y) is divisible by X.
- (N+X) is divisible by Y.
Find any awesome integer N. We can prove that under the given constraints, an answer always exists.
Note that you do not have to minimize the answer.
Input Format
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of a single line of input.
- The first line of each test case contains two space-separated integers XX and YY — favorite numbers of Chef’s nephews.
Output Format
For each test case, output an awesome integer. If there are several answers, you may print any of them.
Constraints
- 1≤T≤10^5
- 1≤X,Y≤10^9
Sample 1:
Input:
3 18 42 1 1 100 200
Output:
192 5 500
Explanation:
Test Case 1: In this case, N = 192 is an awesome integer because:
- 1≤192≤1018
- 192 + 18=210 is divisible by 4242
- 192 + 42=234 is divisible by 1818
Test Case 2: We can output any positive integer NN such that (1≤N≤10^18) because N + 1 is divisible by 1 for all such N.
Solution
/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
while (T-->0) {
long a = sc.nextLong();
long b = sc.nextLong();
boolean printed = false;
if (a != b) {
long N = a*b - a-b;
if (N<0) {
N = 2*a*b-a-b;
}
System.out.println(N);
printed = true;
}
if (printed == false) {
System.out.println(a);
}
}
}
}
Happy Learning – If you require any further information, feel free to contact me.