This page is still under construction.

Parts of this page are still being built. What you see may change.

Number tag exchange

Interview

Time limit2sMemory limit512 MB

Summary
Simulate M bubble passes over the line, swapping neighbors when the front tag has the larger remainder modulo the pass number.
Level

Easy2 of 10

Topics
Simulation, Implementation
Solved
No attempts yet

Problem

NN students stand in one line in a classroom. The ii-th student from the front holds one number tag, and the number written on it is AiA_i.

There are MM cards, numbered 1 to MM. The cards are used in order starting from card 1, and card kk (2≤k≤M2 \le k \le M) is used after card k−1k-1 has been used.

Card ii is used like this.

  • The teacher gives card ii to student 1.
  • Student jj, who holds the card, passes it to student j+1j+1. If Aj mod iA_j \bmod i is greater than Aj+1 mod iA_{j+1} \bmod i, the two students exchange their number tags. Here AjA_j is the number on the tag student jj holds at that moment.
  • Once the last student receives the card, that card is thrown away.

The game ends when card MM has been thrown away. Write a program that prints the number tags the students hold after the game ends, in order from the front of the line.

Input

The first line has the number of students NN and the number of cards MM, separated by a space. (1≤N≤1001 \le N \le 100, 1≤M≤1001 \le M \le 100)

Each of the next NN lines has the number AiA_i on the tag that student ii starts with. (1≤Ai≤10001 \le A_i \le 1000)

Output

Print the number on the tag each student holds after the game ends, one per line, in order from the front of the line.

Hint

Suppose six students hold the tags 3, 2, 8, 3, 1, 5 in that order and there are four cards. The line changes as each card is used.

  • after card 1: 3 2 8 3 1 5
  • after card 2: 2 8 3 3 1 5
  • after card 3: 2 3 3 1 8 5
  • after card 4: 2 3 1 8 5 3

Examples2

  1. Example 1

    Input
    6 4
    3
    2
    8
    3
    1
    5
    
    Expected output
    2
    3
    1
    8
    5
    3
    
  2. Example 2

    Input
    10 6
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    
    Expected output
    6
    1
    2
    3
    10
    4
    8
    7
    9
    5