Inverted Josephus
InterviewTime limit1sMemory limit1024 MB
Simulate removing every K-th person from a circle of N, reversing direction after each block of M removals, and print the removal order.
- Level
Medium4 of 10
- Topics
- Simulation, Array, Linked list, Implementation
- Solved
- No attempts yet
Problem
The Josephus problem goes as follows.
Person sits to the right of person , person sits to the right of person , and in the same way people sit in a circle. Person sits to the right of person . First, person () is removed, and after that the -th person to the right of the person removed just before is removed repeatedly. When everyone has been removed, in what order are the people removed?
The answer to this problem is called the (, )-Josephus permutation, and the (, )-Josephus permutation is .
Going around in only one direction the whole time is too boring, though. So to make the Josephus problem more interesting, the direction of rotation reverses every time people have been removed. The answer to this newly defined problem is called the (, , )-inverted Josephus permutation, and the (, , )-inverted Josephus permutation is .
Given , , and , compute the (, , )-inverted Josephus permutation.
Input
The first line gives the integers , , and . (, )
Output
Print the numbers of the (, , )-inverted Josephus permutation in order, one per line.