#P8671. Josephus Problem

    ID: 21837 Type: Default 1000ms 256MiB

Josephus Problem

Josephus Problem

There are n people numbered from 1 to n standing in a circle arranged in clockwise order. Starting with person number 1, they count off in order (starting from 1). Whenever the count reaches k, the corresponding person leaves the circle. The next person then starts counting from 1 again.

This problem is known as the famous Josephus problem. Given values for n and k, your task is to determine the number of the last remaining person.

inputFormat

The input consists of two integers n and k separated by a space, where 1 ≤ n, k ≤ 10^6.

outputFormat

Output the number of the last remaining person.

sample

7 3
4