#P11062. Minimizing the Absolute Difference
Minimizing the Absolute Difference
Minimizing the Absolute Difference
You are given two integers \(a\) and \(b\) (which may be negative). You are allowed to perform any number of operations (possibly zero) from the following two choices in each move:
- Operation 1: Assign \(a \gets a+b\).
- Operation 2: Assign \(b \gets a+b\).
Your goal is to minimize the absolute difference \(\lvert a-b \rvert\). In other words, you want to achieve the smallest possible non-negative number by repeatedly replacing one of the two numbers with their sum. Note that if at some point you can make both numbers equal you obtain \(\lvert a-b \rvert=0\), which is optimal.
Observation and Hint:
At each move, note that when you perform Operation 1 the new difference becomes \(\lvert (a+b)-b \rvert=\lvert a\rvert\), and when you perform Operation 2 it becomes \(\lvert a-(a+b) \rvert=\lvert b\rvert\). In many cases it is optimal to make a move so that the difference becomes one of the absolute values \(|a|\) or \(|b|\). However, when \(a\) and \(b\) have opposite signs, the sum \(a+b\) can be even smaller in absolute value because of cancellation. In summary, the answer can be determined by the following strategy:
- If \(a = b\), output 0.
- If \(a\) and \(b\) have the same sign (or one of them is 0), then the best you can achieve is \(\min(\lvert a\rvert,\lvert b\rvert,\lvert a-b\rvert)\).
- If \(a\) and \(b\) have opposite signs then the optimal answer is \(\min(\lvert a\rvert, \lvert b\rvert, \lvert a+b \rvert)\) since \(\lvert a-b\rvert = \lvert a\rvert+\lvert b\rvert\) in that case.
Your task is to implement this logic.
inputFormat
The input consists of a single line containing two space‐separated integers \(a\) and \(b\).
outputFormat
Output a single integer representing the minimum possible value of \(\lvert a-b \rvert\) that can be achieved through the allowed operations.
sample
3 6
3