Restore the Original (Large)
Time limit3sMemory limit1024 MB
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 cards, each labeled with one of the numbers .
There is a sequence containing each number from 1 to N exactly once. For each , taking the -th card to the -th position is called a shuffle.
For example, suppose is 1, 4, 5, 3, 2 and is 4, 3, 1, 2, 5. Shuffling these cards once gives 3, 5, 1, 4, 2. This is shown in the figure below. below denotes the state after shuffling the cards once.

Given the state of the cards after shuffling them times in this way and the values of , find the arrangement the cards originally had.
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, representing the arrangement of the cards after shuffling them times.
The third line gives values separated by spaces.
Output
Print the values of the original card arrangement, through , separated by spaces.
Constraints
- is an integer