Josephus Permutation
InterviewTime limit2sMemory limit512 MB
Remove every Kth person from a circle of N people and print the removal order in angle brackets.
- Level
Easy3 of 10
- Topics
- Queue, Simulation
- Solved
- No attempts yet
Problem
people numbered 1 to sit in a circle, and a positive integer () is given. Starting the count at person 1, remove the th person from the circle. Once a person is removed, count again from 1 at the next person, remove the th person, and repeat the same process along the circle of the remaining people. The process continues until all people are removed. The order in which the people are removed is the -Josephus permutation. For example, the -Josephus permutation is <3, 6, 2, 7, 5, 1, 4>.
Given and , write a program that finds the -Josephus permutation.
Input
The first line contains and in this order, separated by a space. ()
Output
Print the Josephus permutation on one line. Start with the opening angle bracket <, write the numbers in the order they were removed, separate consecutive numbers with a comma and one space, and finish with the closing angle bracket >.