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 999999999output
0 1 12 499999999Note
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(); } }