#B3766. Children Line Reordering
Children Line Reordering
Children Line Reordering
There are \( n \) children standing in a line. They are assigned a unique student number each, given in order from left to right as \( a_1, a_2, \ldots, a_n \). The physical education teacher issues \( T \) commands one by one. Each command consists of a positive integer \( k \) (with \( k \le n \)).
For each command, the children rearrange themselves as follows (considering the current order before the command):
- The children at positions \( 1, \; k+1, \; 2k+1, \; \ldots \) (i.e. the first child, the \( (k+1) \)th child, the \( (2k+1) \)th child, etc.) will stand in the new line from left to right as the first group.
- If \( k \ge 2 \), the children at positions \( 2, \; k+2, \; 2k+2, \; \ldots \) will form the second group and stand immediately to the right of the first group in the same order.
- If \( k \ge 3 \), the children at positions \( 3, \; k+3, \; 2k+3, \; \ldots \) will form the third group and stand immediately to the right of the second group.
- This process continues with the \( r \)th group (for \( 1 \le r \le k \)) consisting of children at positions \( r, \; k+r, \; 2k+r, \; \ldots \) until all children have been arranged.
After applying \( T \) such commands in sequence, determine the final order of the children based on their student numbers.
inputFormat
The first line contains two integers \( n \) and \( T \) separated by a space.
The second line contains \( n \) integers \( a_1, a_2, \ldots, a_n \), representing the initial order of the children's student numbers from left to right.
Each of the next \( T \) lines contains a single integer \( k \) (with \( 1 \le k \le n \)) representing a command.
outputFormat
Output a single line containing \( n \) integers representing the final order of the children's student numbers after all \( T \) commands have been executed. The numbers should be separated by a space.
sample
5 1
1 2 3 4 5
2
1 3 5 2 4