ABC161 C - Replacing Integer
Aoki can perform the following operations on any integer $ x $.
Operation
Replace $ x $ with the absolute value of the difference between $ x $ and $ K $.
An initial value of the integer N is given. Find the minimum value of N that can be taken when the above operation is performed 0 or more times on this integer as many times as you like.
Constraints
For example, when $ N = 7 $ and $ K = 4 $, the operation to be performed is to subtract $ K $ from $ x $ until the absolute value of the difference is minimized, so $ x = 7, x = 3, x = 1,x = 3 $ and $ x $ change. Therefore, the calculated $ x $ is $ 1 $. After $ x = 1 $, $ x $ repeats $ 1 $ and $ 3 $.
However, it is difficult to repeat this operation honestly when $ N $ and $ K $ are large, so the minimum value to be calculated is the remainder of $ N / K $ or the remainder of $ K-(N / K) $. To do.
N, K = map(int,input().split())
print(min(N % K, K - (N % K)))
Please point out any mistakes.
Recommended Posts