codeforces 1603B. Moderate Modular Mode

B. Moderate Modular Mode

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.

题意:给你两个数a和b,让你找到一个数x,使得 x%a=b%x

个人思路:当时看例子就觉得最后输出的答案应该是a*b+b,这样第一个式子是b,第二个也是b,然后代码一打出来第一个例子就寄了.然后觉得发现这种方式只能是a>b的情况下才能得出,然后得出当a小于b的时候,那个x应该是在a和b之间的

思路:
(1).a>b时,x=a+b(官方思路简单多了,哎蒻蓟)
(2).a=b时,x=a/x=b;
(3).a<b时,x=y-y%x/2
来说说第三个,我们要在a和b之间找到一个数使得式子成立 a>x&&x<b 也就是说求余之后数值都变了
根据这点,我们在想想a到b是不是有一段距离,b%a这是a和b之间两个数都没办法到达的地方,那么这个地方平分不就可以得到最后答案吗 (下图解释)
请添加图片描述

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6+10,mod=998244353,INF=0x3f3f3f3f;
int a[N],b[N];
int main()
{
	int t;cin>>t;
	while(t--)
	{
		int x,y;cin>>x>>y;

		if(x<=y)cout<<y-(y%x)/2<<endl;
		else cout<<x+y<<endl;
	}
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值