标签:cpp http ble 如何 ram HERE display 式表 from
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.
给定3个整数x,y和n,找到一个数k,使得
,并且
。
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.
给定t组测试数据,每组数据保证存在这样的k。
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.
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.
7
7 5 12345
5 0 4
10 5 15
17 8 54321
499999993 9 1000000000
10 5 187
2 0 999999999
12339
0
15
54306
999999995
185
999999998
比如输入x=7,y=5,n=12345,如何求得k,使得k<12345并且k%7=5呢?
朴素的做法:遍历n到1,看k%7是否等于5
换一种思路:考虑p%7是否等于0,如果等于0,p再加上5不就是答案k了吗。显然直接用n/b取底再乘以b即是最大的p。结果可以用公式表示如下:
当我们在解决一个问题的时候,首先看能不能将这个问题转换为相近的问题,间接的去思考最终结果,虽然是一个很小的题目,但是可以多花一点时间思考为什么。
每天学习一点点,你学会了吗,这是CF最简单的题目啦,加油~
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
int t; cin >> t;
ll x, y, n;
while (t--) {
cin >> x >> y >> n;
cout << (n - y) / x * x + y << endl;
}
return 0;
}
标签:cpp http ble 如何 ram HERE display 式表 from
原文地址:https://www.cnblogs.com/RioTian/p/13395004.html