#C8636. The Last Person Standing

    ID: 52640 Type: Default 1000ms 256MiB

The Last Person Standing

The Last Person Standing

In this problem, you are given two integers n and k, where n represents the number of people arranged in a circle and k is the count for consecutive eliminations. Starting from the first person, every kth person is eliminated from the circle until only one person remains. The task is to determine the position (1-indexed) of the last person remaining.

The recurrence relation for this problem, often known as the Josephus problem, is given by:

$$J(n, k) = \begin{cases} 1 & \text{if } n = 1,\\ ((J(n-1, k) + k - 1) \mod n) + 1 & \text{if } n > 1. \end{cases}$$

Implement the function to compute this value efficiently.

inputFormat

The input consists of two space-separated integers:

  • n: the number of persons in the circle.
  • k: the step rate determining which person is eliminated in each round.

outputFormat

Output a single integer representing the position (1-indexed) of the last person remaining.

## sample
5 2
3