A. The Miracle and the Sleeper(水题)

这篇博客探讨了一个算法问题,即在给定整数范围内找到使a mod b达到最大值的a和b。作者提供了AC代码,并解释了如何在不能取到a/2+1的情况下选择直接进行模运算。博客还包含了多个测试用例及其解决方案,并分享了个人的学习心得,鼓励读者跟上课程进度,不断练习编程技巧。
摘要由CSDN通过智能技术生成

题目如下:

You are given two integers l and r, l≤r. Find the largest possible value of amodb over all pairs (a,b) of integers for which r≥a≥b≥l

As a reminder, amodb

is a remainder we get when dividing a by b. For example, 26mod8=2

Input

Each test contains multiple test cases.

The first line contains one positive integer t

(1≤t≤104)

, denoting the number of test cases. Description of the test cases follows.

The only line of each test case contains two integers l

, r (1≤l≤r≤109).

Output

For every test case, output the largest possible value of amodb

over all pairs (a,b) of integers for which r≥a≥b≥l.

Example

Input

4
1 1
999999999 1000000000
8 26
1 999999999

Output

0
1
12
499999999

Note

In the first test case, the only allowed pair is (a,b)=(1,1)

, for which amodb=1mod1=0.

In the second test case, the optimal choice is pair (a,b)=(1000000000,999999999)

, for which amodb=1.

AC代码如下:

#include<stdio.h>
int main()
{
	int t;
	scanf("%d", &t);
	int x, y;
	while (t--)
	{
		scanf("%d %d", &x, &y);
		if (y / 2 + 1 >= x)
		{
			printf("%d\n", y %(y/2+1));
		}
		else
			printf("%d\n", y % x);
	}
	return 0;
}

对于a%b,如果要获取最大的值

理想上:

1.a越大越好

2.b = a/2+1

但也有可能存在a/2+1找不到的情况(代码上:y / 2 + 1 < x [对应else]),那就直接进行mod——》

y%x。此时的x——》就是最理想的(因为你无法取到y/2+1,x就是该输入中最理想的)!

ps:开学的第4天,已经开始正常上课了。课程跟紧,代码不停!ACM努力中!!!

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Joanh_Lan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值