AI智能
改变未来

Required Remainder带余除法(初等数论)

Required Remainder
描述

You are given three integersx,yandn. Your task is to findthe maximumintegerksuch that0≤k≤n 0≤k≤nthatk mod x=yk mod x=y, wheremodis modulo operation. Many programming languages use percent operator % to implement it.

In other words, with givex, y,andnyou need to find the maximum possible integer from0tonthat has the remainderymodulox.

You have to answertindependent test cases. It is guaranteed that suchkexists for each test case.

输入
The first line of the input contains one integert(1≤t≤5⋅1041≤t≤5⋅104) — the number of test cases. The nexttlines contain test cases.

The only line of the test case contains three integersx,yandn(2≤x≤109;0≤y<x;y≤n≤1092≤x≤109;0≤y<x;y≤n≤109).

It can be shown that suchkalways exists under the given constraints.

输出
For each test case, print the answer —maximum non-negativeintegerkksuch that0≤k≤n0≤k≤nandkmodx=ykmodx=y. It is guaranteed that the answer always exists.

输入样例 1

7
7 5 12345
5 0 4
10 5 15
17 8 54321
499999993 9 1000000000
10 5 187
2 0 999999999
输出样例 1

12339
0
15
54306
999999995
185
999999998
来源

codeforce
题目大意
T 组询问,求 ≤n的最大的 k 使得 k%x=y 的 正整数。

T≤5×10 4 ,x,y,n≤10 9
.思维题,自己列几个例子就能有思路

#include<bits/stdc++.h>using namespace std;int main(){int t;cin>>t;while(t--){int x,y,n;cin>>x>>y>>n;if(n%x>=y)cout<<(n/x)*x+y<<endl;elsecout<<(n/x-1)*x+y<<endl;}}
赞(0) 打赏
未经允许不得转载:爱站程序员基地 » Required Remainder带余除法(初等数论)