博客
关于我
按需取余
阅读量:404 次
发布时间:2019-03-06

本文共 1591 字,大约阅读时间需要 5 分钟。

CF 1374A. Required Remainder

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,使得

\[0⩽k⩽n\]

,并且

\[k \% x = y\]

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。

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

输入

77 5 123455 0 410 5 1517 8 54321499999993 9 100000000010 5 1872 0 999999999

输出

1233901554306999999995185999999998

比如输入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。结果可以用公式表示如下:

\[k=⌊n÷x⌋∗x+y\]

当我们在解决一个问题的时候,首先看能不能将这个问题转换为相近的问题,间接的去思考最终结果,虽然是一个很小的题目,但是可以多花一点时间思考为什么。

每天学习一点点,你学会了吗,这是CF最简单的题目啦,加油~

#include
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;}

转载地址:http://yuykz.baihongyu.com/

你可能感兴趣的文章
MySQL一站到底!华为首发MySQL进阶宝典,基础+优化+源码+架构+实战五飞
查看>>
MySQL万字总结!超详细!
查看>>
Mysql下载以及安装(新手入门,超详细)
查看>>
MySQL不会性能调优?看看这份清华架构师编写的MySQL性能优化手册吧
查看>>
MySQL不同字符集及排序规则详解:业务场景下的最佳选
查看>>
Mysql不同官方版本对比
查看>>
MySQL与Informix数据库中的同义表创建:深入解析与比较
查看>>
mysql与mem_细说 MySQL 之 MEM_ROOT
查看>>
MySQL与Oracle的数据迁移注意事项,另附转换工具链接
查看>>
mysql丢失更新问题
查看>>
MySQL两千万数据优化&迁移
查看>>
MySql中 delimiter 详解
查看>>
MYSQL中 find_in_set() 函数用法详解
查看>>
MySQL中auto_increment有什么作用?(IT枫斗者)
查看>>
MySQL中B+Tree索引原理
查看>>
mysql中cast() 和convert()的用法讲解
查看>>
mysql中datetime与timestamp类型有什么区别
查看>>
MySQL中DQL语言的执行顺序
查看>>
mysql中floor函数的作用是什么?
查看>>
MySQL中group by 与 order by 一起使用排序问题
查看>>