You are given three integers x, y and n. Your task is to find the maximum integer k such that 0 ≤ k ≤ n that k mod x = 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 ⋅ 10^4) — 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 ≤ 10^9;~ 0 ≤ y < x;~ y ≤ n ≤ 10^9).
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 k mod x = y. It is guaranteed that the answer always exists.
In the first test case of the example, the answer is 12339 = 7 ⋅ 1762 + 5 (thus, 12339 mod 7 = 5). It is obvious that there is no greater integer not exceeding 12345 which has the remainder 5 modulo 7.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We are given t test cases. For each, given x, y, n, find maximum k such that 0 ≤ k ≤ n and k % x = y. It is guaranteed such k exists.
We need to compute this efficiently. The straightforward: find the largest number ≤ n with remainder y modulo x. That's essentially: let k = n - ((n - y) % x). But careful: if n % x == y, then k = n. Otherwise, we need to subtract the difference to get remainder y. More precisely, we want the largest k ≤ n such that k ≡ y mod x. That is k = n - ((n - y) mod x). But if (n - y) mod x yields something from 0 to x-1. But if n - y is negative? Since y ≤ n, it's non-negative. So n - y >= 0. Then (n - y) % x gives r = (n - y) mod x. Then k = n - r. But check: Does k mod x equal y? Let's verify: k = n - r, where r = (n - y) mod x. Since (n - y) ≡ r (mod x) and 0 ≤ r < x, then n - r ≡ y (mod x). Also k ≤ n. And k is maximum? Suppose there is a larger k' > k and ≤ n with same remainder. Then k' = k + m x, with m >=1. Then k' ≤ n, so k + x ≤ n => n - r + x ≤ n => x ≤ r, but r < x, contradiction unless r=0. If r=0, then k = n, which is maximum. So formula works.
But note: The condition guarantees existence. It might fail if n < y? But given y ≤ n, so fine.
Thus answer = n - ((n - y) % x). But careful: If n % x >= y, then (n - y) % x = (n%x - y) if n%x >= y, else (n%x - y + x). Actually the formula works.
Implementation: For each test case, read x, y, n. Compute ans = n - ((n - y) % x). Output ans.
But ensure the result is ≤ n and ≥ 0? Since n >= y, and (n - y) % x is non-negative < x, so ans >= n - (x-1) which could be less than 0? But since y <= n, and we need k such that remainder y, minimal such k is y itself. But ans might be less than y? Let's check: if n - ((n-y)%x) could be less than y? Example: x=7, y=5, n=6? But constraint y ≤ n, so n could be 6, y=5. Then (n - y) =1, %7=1, ans=6-1=5, which is equal to y. So ans >= y? Actually ans = n - r, with r = (n - y) mod x. Since r <= n - y (because (n-y) mod x <= n-y when n-y < x? Not always: if n-y >= x, then (n-y)%x can be any less than x, but n-y could be large; r can be as large as x-1, while n-y might be small? Actually r is always less than x, and n-y could be less than x, then r = n-y, so ans = n - (n-y) = y. If n-y >= x, then r can be less than x, but n - r > n - (x-1) which could be less than y? Possibly if n is just slightly bigger than y? Let's analyze: If n-y >= x, then n >= y+x. The minimal ans in that case: worst-case r = x-1, then ans = n - (x-1). Since n >= y+x, we have ans >= y+1, so > y. So ans >= y. So it's fine.
But guarantee that such k exists: Since y ≤ n, we have at least k=y (since y mod x = y because y < x). So answer exists. So formula works.
Double-check example:
x=7,y=5,n=12345: n-y=12340, %7? 12340 mod 7: 71762=12334, remainder 6? Actually 71762 = 12334, remainder 6. So r=6, ans=12345-6=12339. matches.
x=17,y=8,n=54321: n-y=54313, %17: compute? 173195=54315? Actually 173195=54315? 173000=51000, 17195=3315, total 54315. That's 2 more than 54313, so remainder? 54313 - 173195? 173195 = 54315, so 54313 = 54315 -2, so remainder = 17 -2 =15? Wait careful: 54313 mod 17. 173195 = 54315, which is larger, so we need 173194 = 173194. 173194 = 17*(3200-6) = 54400 -102 = 54298. Then 54313 - 54298 = 15. So r=15, ans=54321-15=54306. matches.
x=499999993,y=9,n=1000000000: n-y=999999991, mod 499999993: 499999993*2 = 999999986, remainder = 5. So r=5, ans=1000000000-5=999999995. matches.
We need to implement for up to 5e4 test cases, numbers up to 1e9, so calculations fit in 64-bit (like long long). Use (n - y) % x. In C++, careful: (n - y) could be up to 1e9, fine; modulo x up to 1e9; but int may be 32-bit, but 1e9 fits in int (max ~2.147e9) for signed? Actually 1e9 < 2.147e9, so int is fine, but to avoid overflow in intermediate, we can use long long.
Simplest: use long long.
Implementation:
int t;
cin >> t;
while(t--) {
long long x, y, n;
cin >> x >> y >> n;
long long r = (n - y) % x;
long long ans = n - r;
cout << ans << endl;
}
Edge Cases:
When n == y: then n - y = 0, r=0, ans = n = y, which works.
When x is larger than n? For example, x=10, y=5, n=5? But y ≤ n and y < x, so n could be 5, x=10 > n. Then n - y = 0, r=0, ans=5, which is y, and works because 5 % 10 =5.
When x = something large, but n - y may be negative? Not because y ≤ n.
So all good.
Time: O(t). Memory: O(1).
We can also note: ans = ((n - y) / x) * x + y. That is another expression: floor((n - y)/x) * x + y. Since we want the largest k ≤ n, it's essentially that. Because we can let m = floor((n - y)/x), then k = mx + y. Since m = floor((n - y)/x) ensures mx + y ≤ n. And maximum. That also works: k = ((n - y) / x) * x + y. Using integer division. This may be simpler: ans = ( (n - y) / x ) * x + y.
Now generate final answer in proper format. ```cpp
#include
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
code
int t;
cin >> t;
while (t--) {
long long x, y, n;
cin >> x >> y >> n;
// Maximum k ≤ n such that k % x == y
long long r = (n - y) % x;
long long ans = n - r;
cout << ans << '\n';
}
return 0;