This page is still under construction.

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

Restore the Original (small)

Interview

Time limit1sMemory limit1024 MB

Summary
Given the deck after K shuffles and the shuffle order D, recover the original arrangement by applying the inverse shuffle K times.
Level

Medium5 of 10

Topics
Array, Simulation, Implementation, Math
Solved
No attempts yet

Problem

There are NN cards, each with one of the numbers P1,P2,⋯ ,PNP_1, P_2, \cdots, P_N written on it.

There is a sequence D1,D2,⋯ ,Di,⋯ ,DND_1, D_2, \cdots, D_i, \cdots, D_N that contains each number from 1 to NN exactly once. For each ii, the operation of taking the DiD_i-th card to position ii is called a shuffle.

For example, suppose P1,P2,⋯ ,PNP_1, P_2, \cdots, P_N is 1, 4, 5, 3, 2 and D1,D2,⋯ ,DND_1, D_2, \cdots, D_N is 4, 3, 1, 2, 5. Shuffling this deck once gives 3, 5, 1, 4, 2. In the figure below, SS is the deck after one shuffle.

You know the deck after KK shuffles by this method, and you know DD. Find the original arrangement of the cards.

Input

The first line gives the number of cards NN and the number of shuffles KK, separated by a space.

The second line gives NN values SiS_i, separated by spaces, describing the deck after KK shuffles.

The third line gives NN values DiD_i, separated by spaces.

Output

Print the original arrangement, the values P1P_1 through PNP_N, separated by spaces.

Constraints

  • 1≤N≤1041 \le N \le 10^4
  • 1≤K≤1031 \le K \le 10^3
  • 1≤Di≤N1 \le D_i \le N
  • 1≤Pi,Si≤1061 \le P_i, S_i \le 10^6
  • PiP_i is an integer

Examples2

  1. Example 1

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

    Input
    4 1
    4 3 2 1
    4 3 2 1
    
    Expected output
    1 2 3 4