#P7485. The Last Salvaged Maple Leaf
The Last Salvaged Maple Leaf
The Last Salvaged Maple Leaf
Winter adores maple leaves. In front of her house, there is a maple tree on which n leaves have fallen, numbered from \(1\) to \(n\). To prevent the leaves from being crushed and decaying on the ground, she intends to pick them up using a peculiar method called Salvage.
The process is as follows:
- Initially, all leaves are uncollected and are considered in a set.
- In each Salvage round, she first sorts the remaining leaves. The sorting order alternates with the rounds: the first round is in ascending order and then alternates between descending and ascending order with each subsequent round.
- After sorting, she picks the first leaf in the sorted order, and then, starting from that position, she picks every \(k+1\)-th leaf. Formally, if the sorted order is \(a_1, a_2, \dots, a_m\), she picks \(a_1, a_{1+k+\,\,}, a_{1+2(k+1)}, \dots\) until no more leaves can be picked in this round.
- The picked leaves are removed from the set. The process continues with the next round (using the alternate order) until all leaves have been picked.
The very last leaf picked represents "yearning" (思念) and is believed to bring happiness. Winter will ask you, many times, for different values of \(n\) and \(k\) and your task is to determine the number of the last leaf picked.
Note: All formulas are displayed in LaTeX format where applicable.
inputFormat
The input consists of two space-separated integers \(n\) and \(k\), where \(n\) is the number of leaves, and \(k\) denotes the interval for picking leaves.
outputFormat
Output the number of the last leaf picked.
sample
6 1
4