14-2 Josephus permutation

We define the Josephus problem as follows. Suppose that nn people form a circle and that we are given a positive integer mnm \le n. Beginning with a designated first person, we proceed around the circle, removing every mmth person. After each person is removed, counting continues around the circle that remains. This process continues until we have removed all nn people. The order in which the people are removed from the circle defines the (n,m)(n, m)-Josephus permutation of the integers 1,2,,n1, 2, \ldots, n. For example, the (7,3)(7, 3)-Josephus permutation is 3,6,2,7,5,1,4\langle 3, 6, 2, 7, 5, 1, 4 \rangle.

a. Suppose that mm is a constant. Describe an O(n)O(n)-time algorithm that, given an integer nn, outputs the (n,m)(n, m)-Josephus permutation.

b. Suppose that mm is not a constant. Describe an O(nlgn)O(n\lg n)-time algorithm that, given integers nn and mm, outputs the (n,m)(n, m)-Josephus permutation.

(Removed)