Codeforces Round (思维题)

1.题目引入:

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

As a reminder, amodbamodb is a remainder we get when dividing aa by bb. For example, 26mod8=2.

Input

Each test contains multiple test cases.

The first line contains one positive integer tt (1≤t≤104)(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 ll, rr (1≤l≤r≤1091≤l≤r≤109).

Output

For every test case, output the largest possible value of amodbamodb over all pairs (a,b)(a,b) of integers for which r≥a≥b≥lr≥a≥b≥l.

2.样例输出: 

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)(a,b)=(1,1), for which amodb=1mod1=0amodb=1mod1=0.

In the second test case, the optimal choice is pair (a,b)=(1000000000,999999999)(a,b)=(1000000000,999999999), for which amodb=1amodb=1.

3.代码如下: 

#include <bits/stdc++.h>
using i64 = long long;
void solve() 
{
    int l, r;
    std::cin >> l >> r;
    std::cout << std::min(r - l, (r - 1) / 2) << "\n";
}
int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr); 
    int t;
    std::cin >> t;
    while (t--) 
	{
        solve();
    }
    return 0;
}

#include <bits/stdc++.h>
int main()
{
    int test;
    scanf("%d", &test);
    while (test --)
{
        int a, b;
        scanf("%d%d", &a, &b);
        if (2 * a <= b)
        {
            printf("%d\n", (b + 1) / 2 - 1);
        }
        else{
            printf("%d\n", b - a);
        }
    }
    return 0;
}

#include <bits/stdc++.h>
 
void solve() 
{
    int l, r;
    std::cin >> l >> r;
    if (r / 2 + 1 >= l) 
    {
        std::cout << r % (r / 2 + 1) << "\n";
    } else {
        std::cout << r - l << "\n";
    }
}
 
int main() 
{
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(0);
    
    int T;
    std::cin >> T;
    while (T--) 
    {
        solve();
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

风遥~

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

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

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

打赏作者

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

抵扣说明:

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

余额充值