Restore the Original (small)
InterviewTime limit1sMemory limit1024 MB
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 cards, each with one of the numbers written on it.
There is a sequence that contains each number from 1 to exactly once. For each , the operation of taking the -th card to position is called a shuffle.
For example, suppose is 1, 4, 5, 3, 2 and is 4, 3, 1, 2, 5. Shuffling this deck once gives 3, 5, 1, 4, 2. In the figure below, is the deck after one shuffle.

You know the deck after shuffles by this method, and you know . Find the original arrangement of the cards.
Input
The first line gives the number of cards and the number of shuffles , separated by a space.
The second line gives values , separated by spaces, describing the deck after shuffles.
The third line gives values , separated by spaces.
Output
Print the original arrangement, the values through , separated by spaces.
Constraints
- is an integer