CF1374A.Required Remainder

入门

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given three integers x,yx, y and nn. Your task is to find the maximum integer kk such that 0≤k≤n0 \le k \le n that k mod x=yk \bmod x = y, where  mod \bmod is modulo operation. Many programming languages use percent operator % to implement it.

In other words, with given x,yx, y and nn you need to find the maximum possible integer from 00 to nn that has the remainder yy modulo xx.

You have to answer tt independent test cases. It is guaranteed that such kk exists for each test case.

给你三个整数 xx、yy 和 nn。你的任务是找出最大的整数 kk,满足 0≤k≤n0 \le k \le n 且 k mod x=yk \bmod x = y,其中  mod \bmod 表示取模运算。许多编程语言使用百分号操作符 % 来实现该运算。

换句话说,在给定 xx、yy 和 nn 的前提下,你需要找出区间 [0,n][0, n] 内模 xx 余 yy 的最大整数。

你需要回答 tt 个相互独立的测试用例。保证每个测试用例均存在满足条件的 kk。

输入格式

The first line of the input contains one integer tt (1≤t≤5⋅1041 \le t \le 5 \cdot 10^4) — the number of test cases. The next tt lines contain test cases.

The only line of the test case contains three integers x,yx, y and nn (2≤x≤109; 0≤y<x; y≤n≤1092 \le x \le 10^9;~ 0 \le y \lt x;~ y \le n \le 10^9).

It can be shown that such kk always exists under the given constraints.

输入的第一行包含一个整数 tt(1≤t≤5⋅1041 \le t \le 5 \cdot 10^4),表示测试用例的数量。接下来的 tt 行为各测试用例。

每个测试用例仅一行,包含三个整数 x,yx, y 和 nn(2≤x≤1092 \le x \le 10^9;0≤y<x0 \le y \lt x;y≤n≤109y \le n \le 10^9)。

在给定约束条件下,可以证明这样的 kk 恒存在。

输出格式

For each test case, print the answer — maximum non-negative integer kk such that 0≤k≤n0 \le k \le n and k mod x=yk \bmod x = y. It is guaranteed that the answer always exists.

对于每个测试用例,输出答案——即满足 0≤k≤n0 \le k \le n 且 k mod x=yk \bmod x = y 的最大非负整数 kk。保证答案一定存在。

输入输出样例

  • 输入#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

说明/提示

In the first test case of the example, the answer is 12339=7⋅1762+512339 = 7 \cdot 1762 + 5 (thus, 12339 mod 7=512339 \bmod 7 = 5). It is obvious that there is no greater integer not exceeding 1234512345 which has the remainder 55 modulo 77.

在示例的第一个测试用例中,答案为 12339=7⋅1762+512339 = 7 \cdot 1762 + 5(因此,12339 mod 7=512339 \bmod 7 = 5)。显然,在不超过 1234512345 的整数中,不存在比 1233912339 更大的、模 77 余 55 的整数。

输入解题思路,AI测评打分。不知道怎么写?

首页