Josephus again

Time Limit: 1000ms
Memory Limit: 32768KB
This problem will be judged on HDU. Original ID: 3089
64-bit integer IO format: %I64d      Java class name: Main

Description

In our Jesephus game, we start with n people numbered 1 to n around a circle, and we eliminated every k remaining person until only one survives. For example, here's the starting configuration for n = 10, k = 2, The elimination order is 2, 4, 6, 8, 10, 3, 7, 1, 9. So 5 survives.The problem: determine the survivor's number , J(n, k).

Input

There are multiple cases, end with EOF
each case have two integer, n, k. (1<= n <= 10^12, 1 <= k <= 1000)

Output

each case a line J(n, k)

Sample Input

10 2
10 3

Sample Output

5
4

Source

Language: 
Theme: 
Share Code? 

Powered by NB231 | Current Style: .