Josephus Problem 3
InterviewTime limit1sMemory limit16 MB
Find the number of the last person remaining after removing every K-th of N people seated in a circle.
- Level
Medium4 of 10
- Topics
- Math, Dynamic programming
- Solved
- No attempts yet
Problem
The Josephus problem is as follows.
people numbered 1 to sit in a circle, and a positive integer () is given. Counting in order, every -th person is removed. Once a person is removed, the counting continues around the circle formed by the remaining people. The process runs until all people are removed, and the order in which 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 number of the person who remains last.
Input
The first line contains and in that order, separated by a space. ()
Output
Print the number of the person who remains last on the first line.