Joseph
Time limit1sMemory limit128 MB
For each k, find the smallest step size m in the Josephus circle that kills all k bad people before any of the k good people.
- Level
Medium5 of 10
- Topics
- Simulation, Implementation, Brute force, Math
- Solved
- No attempts yet
Problem
The Josephus problem is famous. For those unfamiliar with the original: people numbered stand in a circle, and every -th person is executed; only the last remaining person survives. Joseph was clever enough to choose the position of the last survivor, saving his own life so that he could later tell us about the incident. For example, when and , the people are executed in the order , and person survives.
Now suppose there are good people and bad people. In the circle the first are the good people and the last are the bad people. Determine the smallest such that every bad person is executed before the first good person.
Input
The input consists of several lines, each containing a single integer . The last line contains . You may assume that .
Output
For each in the input, print the corresponding on its own line.