给定一个式子:f(x)=x/a+x%a(其中x/a向下取整)
已知a是给定的,x可以从[l,r]范围中选择一个,要求计算出最大的f(x)
Input
第一行给定一个t(1<=t<=1e4)表示有t组样例
对于每一组样例给定l,r,a(1<=l<=r<=1e9,1<=a<=1e9)
The first line of input data contains an integer t (11≤t≤10^4) — the number of input test cases.
This is followed by t lines, each of which contains three integers l, r and a (1≤l≤r≤10^9,1≤a≤10^9) — the left and right boundaries of the segment and the fixed value of a.
Output
输出最大的f(x),x为[l,r]范围内的一个数
For each test case, output one number on a separate line — the maximum value of the function on a given segment for a given a.
Sample 1
Inputcopy | Outputcopy |
---|---|
5 1 4 3 5 8 4 6 10 6 1 1000000000 1000000000 10 12 8 | 2 4 5 999999999 5 |
Note
In the first sampl
f(1)=⌊13⌋+1mod3=0+1=1
f3(2)=⌊32⌋+2mod3=0+2=2
(3)=⌊33⌋+3mod3=1+0=1
f3(4)=⌊34⌋+4mod3=1+1=2
As an answer, obviously,f3(2) andf3(4) are suitable.
本题 是一个典型的数学题:所以我们首先应该列出数学公式,然后完成程序设计。
f(x)=x/a+x%a (l≤x≤r);
1.r/a=0.
maxf(x)=r;
2.r/a>0;
if (r/a+1)*a-1>r (a!=1)
maxf(x)=(r/a-1)+(a-1);
else maxf(x)=r/a+r%a;
#include<stdio.h>
int main()
{
long long t,l,r,a;
scanf("%lld",&t);
while(t--)
{
scanf("%lld%lld%lld",&l,&r,&a);
long long b=r/a;
if(b==0)
printf("%lld\n",r);
if(b!=0)
{
if(a*b+a-1>r&&(b*a-1)>=l&&a!=1)
printf("%lld\n",(b+a-2));
else
printf("%lld\n",(b+r%a));
}
}
return 0;
}