This page is still under construction.

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

Restore the Original (Large)

Time limit3sMemory limit1024 MB

Summary
Given the card arrangement after K shuffles and the shuffle permutation D, find the original arrangement before any shuffles.
Level

Medium7 of 10

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

Problem

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

There is a sequence D1,D2,⋯ ,Di,⋯ ,DND_1, D_2, \cdots, D_i, \cdots, D_N containing each number from 1 to N exactly once. For each ii, taking the DiD_i-th card to the ii-th position 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 these cards once gives 3, 5, 1, 4, 2. This is shown in the figure below. SS below denotes the state after shuffling the cards once.

Given the state of the cards after shuffling them KK times in this way and the values of DD, find the arrangement the cards originally had.

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, representing the arrangement of the cards after shuffling them KK times.

The third line gives NN values DiD_i separated by spaces.

Output

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

Constraints

  • 1≤N≤1061 \le N \le 10^6
  • 1≤K≤10151 \le K \le 10^{15}
  • 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