Deleting Numbers
Time limit1sMemory limit512 MB
Numbers 1 to n are repeatedly scanned, every k-th remaining number is deleted each round, and you must report which round removes n, or 0 if it survives.
- Level
Medium7 of 10
- Topics
- Math, Simulation, Implementation, Number theory
- Solved
- No attempts yet
Problem
The natural numbers from to are written in a row, and a natural number is given.
One or more steps of deleting numbers in this row are performed. At each step, the remaining numbers are scanned in increasing order, and every -th number is deleted. If fewer than numbers remain after a step, the deletion process ends.
You must determine at which step the number is deleted, or find out that it is not deleted before the process ends.
For example, let and .
- At the first step, the numbers are deleted, leaving .
- At the second step, the numbers are deleted, leaving .
- At the third step, the numbers are deleted, leaving .
- At the fourth step, the number is deleted, leaving . Since one number remains, the process ends.
Thus the number is deleted at the third step.
Write a program that, given the numbers and , determines at which step the number is deleted.
Input
The first line of the input contains an integer ().
The second line of the input contains an integer (, ).
Output
Output a single integer: the number of the step at which the number is deleted, or if the number is not deleted.