#P8674. [蓝桥杯 2018 国 B] 调手表

[蓝桥杯 2018 国 B] 调手表

Description

Xiaoming bought a high-end electronic watch and is getting ready to set the time.

In the M78 Nebula, the units of time are different from those on Earth: one hour in the M78 Nebula has nn minutes.

As everyone knows, the watch has only one button that can increase the current number by 11. When setting the minutes, if the current displayed number is 00, pressing the button once changes it to 11, pressing again changes it to 22. If the current number is n1n-1, then after pressing once it becomes 00.

Because Xiaoming is very particular, he must set the watch to the exact correct time. If the time on the watch is 11 minute ahead of the current time, then he needs to press the +1+1 button n1n-1 times to set it back to the correct time.

Xiaoming thinks it would be great if the watch could have one more button that adds kk to the current number.

He wants to know: if there is a +k+k button, and he presses buttons using the optimal strategy, then from any minute value to any other minute value, what is the maximum number of button presses needed.

Note: when pressing the +k+k button, if the number after adding kk exceeds n1n-1, it will be taken modulo nn.

For example, when n=10,k=6n=10,k=6, if the current time is 00, then pressing the +k+k button twice will set it to 22.

Input Format

One line with two integers n,kn,k, as described above.

Output Format

One line with one integer, meaning: using the optimal strategy, the maximum number of button presses needed to adjust from one time to another.

5 3
2

Hint

Sample Explanation

If the time is already correct, press 00 times. Otherwise, the relationship between the number of presses and the sequence of operations is as follows:

  1. +1
  2. +1, +1
  3. +3
  4. +3, +1

Constraints

For 30%30\% of the testdata, 0<k<n50<k<n \le 5.

For 60%60\% of the testdata, 0<k<n1000<k<n \le 100.

For 100%100\% of the testdata, 0<k<n1050<k<n \le 10^5.

Time limit: 3 seconds, 256M. Lanqiao Cup 2018, the 9th National Final.

Translated by ChatGPT 5