[codeforces 1374A] Required Remainder 带余除法(该词来自初等数论)

Codeforces Round #653 (Div. 3)   参与排名人数11687

[codeforces 1374A]    Required Remainder    带余除法(该词来自初等数论)

总目录详见https://blog.csdn.net/mrcrack/article/details/103564004

在线测评地址http://codeforces.com/contest/1374/problem/A

ProblemLangVerdictTimeMemory
A - Required Remainder GNU C++17Accepted46 ms3600 KB

题目大意:0<=k<=n,找k%x=y,对应的k的最大值

基本思路:k=a*x+y(带余除法),a=n/x或者a=n/x-1.

样例模拟如下:

7 5 12345

12339
(12345/7)*7+5=1763*7+5=12346>12345,故12346-7=12339

5 0 4

0
(4/5)*5+0=0<4,故输出0

10 5 15

15
(15/10)*10+5=1*10+5=15==15故输出15


17 8 54321

54306
(54321/17)*17+8=3194*17+8=54306<54321故输出54306

499999993 9 1000000000

999999995
(1000000000/499999993)*499999993+9
=2*499999993+9=999999995<1000000000
故输出999999995

10 5 187

185
(187/10)*10+5=18*10+5=185

2 0 999999999

999999998
(999999999/2)*2+0=499999999*2+0=999999998

AC代码如下

#include <stdio.h>
int main(){
	int t,x,y,n,a,b;
	scanf("%d",&t);
	while(t--){
		scanf("%d%d%d",&x,&y,&n);
		a=n/x;
		b=a*x+y;
		if(b>n)b-=x;
		printf("%d\n",b);
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值