forked from sureshmangs/Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1374A - Required Remainder.cpp
60 lines (46 loc) · 1.54 KB
/
1374A - Required Remainder.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
You are given three integers x,y and n. Your task is to find the maximum integer k such that 0=k=n that kmodx=y, where mod is modulo operation. Many programming languages use percent operator % to implement it.
In other words, with given x,y and n you need to find the maximum possible integer from 0 to n that has the remainder y modulo x.
You have to answer t independent test cases. It is guaranteed that such k exists for each test case.
Input
The first line of the input contains one integer t (1=t=5·104) — the number of test cases. The next t lines contain test cases.
The only line of the test case contains three integers x,y and n (2=x=109; 0=y<x; y=n=109).
It can be shown that such k always exists under the given constraints.
Output
For each test case, print the answer — maximum non-negative integer k such that 0=k=n and kmodx=y. It is guaranteed that the answer always exists.
Example
inputCopy
7
7 5 12345
5 0 4
10 5 15
17 8 54321
499999993 9 1000000000
10 5 187
2 0 999999999
outputCopy
12339
0
15
54306
999999995
185
999999998
Note
In the first test case of the example, the answer is 12339=7·1762+5 (thus, 12339mod7=5). It is obvious that there is no greater integer not exceeding 12345 which has the remainder 5 modulo 7.
*/
#include<bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
int x, y, n;
cin >> x >> y >> n;
if (n % x >= y) cout << n - (n % x) + y << endl;
else cout << n - (n % x) - x + y << endl;
}
return 0;
}