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
Problem | Lang | Verdict | Time | Memory |
---|---|---|---|---|
A - Required Remainder | GNU C++17 | Accepted | 46 ms | 3600 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;
}