#P2130. Minimal Time to Reach Homework
Minimal Time to Reach Homework
Minimal Time to Reach Homework
Wzf starts at the point (1, 1) and every second he must move exactly \(2^{i-1}\) steps in one of the four cardinal directions (up, down, left, or right), where \(i\) is the index of the current second (starting from 1). Given the coordinates of the homework, determine the minimum number of seconds required for him to reach that destination. If it is impossible to reach, output -1.
Explanation:
- On the 1st second, the step length is \(2^0 = 1\). The move adds an odd displacement to one coordinate while leaving the other unchanged.
- As a result, one of the coordinate differences \(x-1\) or \(y-1\) must be odd and the other even, which is a necessary condition for reachability.
- If the target is reachable, the total distance moved in \(k\) seconds is \(2^k-1\). Thus, we must have \(|x-1|+|y-1| \le 2^k-1\) for some minimum \(k\).
inputFormat
The input consists of a single line containing two space-separated integers \(x\) and \(y\), which represent the coordinates of the homework.
outputFormat
Output the minimum number of seconds required to reach the destination. If it is impossible to reach the target under the given conditions, output -1.
sample
2 1
1