Codeforces Round 752 (Div. 1) B
YouKn0wWho has two even integers x and y. Help him to find an integer n such that 1≤n≤2⋅1018 and nmodx=ymodn. Here, amodb 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≤t≤105) — the number of test cases.
The first and only line of each test case contains two integers x and y (2≤x,y≤109, both are even).
Output
For each test case, print a single integer n (1≤n≤2⋅1018) 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.
Example
input
Copy
4
4 8
4 2
420 420
69420 42068
output
Copy
4
10
420
9969128
Note
In the first test case, 4mod4=8mod4=0.
In the second test case, 10mod4=2mod10=2.
In the third test case, 420mod420=420mod420=0.
题意: 给两个数要你找到一个x让nmodx=ymodn
思路:
我们可以发现当 n > m 时,m%x 这个范围是(0~m-1),而(n+m)%n == (m%n),而n>m,所以x = n+m
当n == m 时,x == n == m;
当n < m 时,你可以发现如果m是n的倍数,则m%n == 0,m%m == 0,所以x = m;
然后我们发现(6,8),(8, 10),(10,12)这些数据x%n他的范围是(0n-1),m%x的范围是(0x-1);
然后我们发现x一定是大于n的,而这个值一般是(n+m)/2,但是这个只发生在n,m相差小于n,大于则是(m/n*n + m)/2;
代码;
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define sc(x) scanf("%lld",&x)
#define pr(x) printf("%lld\n",x)
#define YES puts("YES");
#define NO puts("NO");
#define Yes puts("Yes");
#define No puts("No");
#define fo(i,j,k) for(int i = j;i <= k;i ++)
#define pre(i,j,k) for(int i = j;i >= k;i --)
#define ls idx*2
#define rs idx*2+1
int pow_mod(int a1, int n1, int m1)
{
if(n1 == 0) return 1;
int x = pow_mod(a1, n1/2, m1);
long long ans = (long long)x * x % m1;
if(n1 % 2 == 1) ans = ans *a1 % m1;
return (int)ans;
}
inline __int128 read(){
__int128 x = 0, f = 1;
char ch = getchar();
while(ch < '0' || ch > '9'){
if(ch == '-')
f = -1;
ch = getchar();
}
while(ch >= '0' && ch <= '9'){
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
inline void print(__int128 x){
if(x < 0){
putchar('-');
x = -x;
}
if(x > 9)
print(x / 10);
putchar(x % 10 + '0');
}
const int N = 1e6 + 10;
int n,m;
void solve()
{
int ans = 0;
cin >> n >> m;
if(n > m)
{
//cout << n + m << '\n';
ans = n+m;
}
else
{
if(m%n)
{
ans = (m/n*n + m)/2;
}
else
ans = n;
//cout << ans << '\n';
}
cout << ans << '\n';
}
signed main()
{
int tt = 1;
cin >> tt;
while(tt--)
{
solve();
}
}