ABC memorandum [ABC161 C --Replacing Integer] (Python)

ABC161 C - Replacing Integer

Problem statement

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

0≤N≤10^{18} 1≤K≤10^{18} All inputs are integers

C - Replacing Integer

solution

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

ABC memorandum [ABC161 C --Replacing Integer] (Python)
ABC memorandum [ABC163 C --managementr] (Python)
ABC memorandum [ABC159 C --Maximum Volume] (Python)
ABC memorandum [ABC158 C --Tax Increase] (Python)
ABC memorandum [ABC157 C --Guess The Number] (Python)
ABC147 C --HonestOrUnkind2 [Python]
ABC memorandum [ABC160 C --Traveling Salesman around Lake] (Python)
ABC163 C problem with python3
ABC188 C problem with python3
ABC187 C problem with python
Solve ABC163 A ~ C with Python
Python memorandum
ABC127 A, B, C Explanation (python)
Python Memorandum 2
ABC166 in Python A ~ C problem
Solve ABC168 A ~ C with Python
Python memorandum
Solve ABC036 A ~ C in Python
python memorandum
python memorandum
Solved AtCoder ABC 114 C-755 with Python3
Solve ABC162 A ~ C with Python
Solve ABC167 A ~ C with Python
ABC128 A, B, C commentary (python)
Solve ABC158 A ~ C with Python
ABC126 A, B, C Explanation (python)
Solve ABC037 A ~ C in Python
Python memorandum
python memorandum
Python memorandum
Solve ABC175 A, B, C in Python
Algorithm in Python (ABC 146 C Binary Search
Beginner ABC154 (Python)
Beginner ABC156 (Python)
Python basics memorandum
Python pathlib memorandum
Python memorandum (algorithm)
AtCoder ABC 174 Python
AtCoder ABC187 Python
AtCoder ABC188 Python
Beginner ABC155 (Python)
python C ++ notes
python, openFrameworks (c ++)
Beginner ABC157 (Python)
Python memorandum [links]
AtCoder ABC 175 Python
Challenge AtCoder (ABC) 164 with Python! A ~ C problem
Python memorandum numbering variables
Python C / C ++ Extension Pattern-Pointer
python memorandum (sequential update)
Python memorandum (personal bookmark)
Next Python in C
Python basic memorandum part 2
Solve ABC169 in Python
Solve Atcoder ABC176 (A, B, C, E) in Python
C API in Python 3
[Python] Iterative processing_Personal memorandum
Memorandum @ Python OR Seminar
python memorandum super basic
[AtCoder explanation] Control ABC180 A, B, C problems with Python!
[AtCoder explanation] Control ABC158 A, B, C problems with Python!