Domo is a super train conductor, he wants to adjust the train he's driving.
There are two instructions below with the description:
1. AddBack N
The AddBack instructions will be followed by N numbers on the seccond line, representing train carriages that you need to add them at the back of the current train.
2. CircularInsert N M
Add N carriages with index M using the circular insert rule.
Example of AddBack:
AddBack 5
1 3 2 4 5
makes the train [4, 1] become [4, 1, 1, 3, 2, 4, 5].
AddBack 3
3 1 2
makes a empty train [] become [3, 1, 2].
Circular insert rule:
1. Repeat steps 2~4 N times. Initially, consider the first carriage as the "NOW" carriage.
2. Insert a carriage with index M right after the NOW carriage.
3. Store the index of the NOW carriage in the variable K.
4. Move forward K steps, and assign the carriage after K steps as the new NOW carriage. (If reaching the end of the train, go to the beginning of the train.)
For example:
suppose the current train is [3, 4, 5, 6] and the instruction is CircularInsert 3 2
1. At the beginning, the NOW carriage is '3', and we insert a carriage '2' right after it.
2. Since NOW carriage is '3', we step forward 3 steps. Hence, the next NOW carriage is '5'.
3. we insert a carriage '2' right after '5'.
4. Since NOW carriage is '5', we step forward 5 steps. Hence, the next NOW carriage is '4'. Notice that we go to the beginning if we step out the last carriage.
5. Insert a carriage '2' right after '5'. The result will be [3, 2, 4, 2, 5, 2, 6].
Notice that if the NOW carriage is the last one, you need to insert it right after it, not at the beginning.
The train is empty in the beginning. Given a series of instructions, please print the index of train carriages after all instructions are executed. The first instruction must be AddBack.
This is a partial judge problem, you're asked to implement these 2 functions.
The input consists of multiple instructions (1 ≤ number of instructions ≤ 103)
the index of each instruction is a positive integer and not greater than 102.
the integer N in both instruction will not greater than 103.
The output only consists of a line denoting the train carriage indices after all the instructions.
It's guaranteed that the output consists of at least one carriage.