Jump
Time limit3sMemory limit128 MB
You remove every k-th number around a circle and print the last three removed numbers for each test case.
- Level
Medium6 of 10
- Topics
- Math, Simulation, Segment tree
- Solved
- No attempts yet
Problem
The natural numbers are placed around a circle in increasing order. We pick numbers one at a time to build a sequence, and counting starts at number .
While the circle still holds numbers, count numbers starting from the current position and take the -th one. Remove it from the circle, append it to the sequence, and start the next count from the number immediately after the one just removed. The sequence built this way is called Jump(n, k). (Here and .)
The first five numbers of Jump(10, 2) are . The next picks are , so Jump(10, 2) = [2, 4, 6, 8, 10, 3, 7, 1, 9, 5]. In the same way, Jump(13, 3) = [3, 6, 9, 12, 2, 7, 11, 4, 10, 5, 1, 8, 13], Jump(13, 10) = [10, 7, 5, 4, 6, 9, 13, 8, 3, 12, 1, 11, 2], and Jump(10, 19) = [9, 10, 3, 8, 1, 6, 4, 5, 7, 2].
Given and , write a program that finds the last three numbers of Jump(n, k). For example, when and , you should print . Note that Jump(1, k) = [1].
Input
The first line contains the number of test cases . Each of the following lines is one test case: two natural numbers and separated by a space. (, )
Output
For each test case, print the third-to-last, second-to-last, and last numbers of Jump(n, k) on one line, separated by spaces.