题目如下:
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努力中!!!