【无标题】

这是一个数学和编程结合的问题,要求在给定的x范围[l,r]内找到使函数f(x)=x/a+x%a达到最大值的x。当r/a=0时,最大值为r;否则,根据a*b+a-1是否大于r来确定最大值是(b-1)*(a-1)还是r/a+r%a。给出的C++代码实现了这个逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

给定一个式子: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

InputcopyOutputcopy
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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值