Problems > The Josephus Survivor

The Josephus Survivor

Difficulty: 1400 2000 ms 128 MB

math implementation

N people stand in a circle, numbered 1..N. Counting starts at person 1; every K-th person is eliminated until one remains. Who survives?

Input: one line with two integers N and K.

Output: the number of the surviving person.

Constraints:

  • 1 ≤ N ≤ 105
  • 1 ≤ K ≤ 109

Sample Tests

Input 1
5 2
Output 1
3
Input 2
1 10
Output 2
1

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.