LoginSignup
0
0

More than 3 years have passed since last update.

Codeforces #653 (Div. 3) A. Required Remainder

Posted at

概要

  • $x, y, n$が与えられる
  • $0 \leq k \leq n$としたとき、$ k$ $mod$ $x = y $となる最大のkを求めよ
  • 制約: $0 \leq y \lt x$ である $y \leq n \leq 10^{9}$

$10, 5, 187$のとき$k=185$である。187以下で10で割って5になる最大の数は185である。

$5, 0, 4$のとき$k=0$である。4以下で5で割って0になる最大の数は0である。

こう考えた

上の例を考察すると、
- $10, 5, 187$のとき$k=185$である。
- $10, 5, 182$のとき$k=175$である。
ことが分かる。

$x * floor(n / x)$ は$n$を超えないxで割ったときの余りが0の整数だから、
$x * floor(n / x) + y$を計算するとxで割ったときの余りがyの整数なので、答えの1つになりうる。

上記の場合、185がそれに該当する。$n$が185未満の場合、185は許容されないので、それより1つ小さい、10で割ってあまり5が候補になる。
これを考えると、$185 - x = 185 - 10 = 175$である、何故なら、
$x * (floor(n / x) - 1) + y$だからである。

コード

from pprint import pprint
import sys
import math
def do():
    x, y, k = map(int, input().split())
    a = x * (math.floor(k / x))
    a += y
    if a > k:
        a -= x
    print(a)
q = int(input())
for _ in range(q):
    do()

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0