Given an , a permutation of is a sequence where each of the numbers occurs exactly once. For example, if , the sequences , and are permutations of .
Given two permutations and of , , , their product is defined as the permutation such that . For example, if , and , then , since:
and ,
and , and
and .
Make a program that, given a permutation and a natural , computes the power of raised to : . By convention, .
The input includes several cases. Each case consists in the number (), followed by numbers between and that describe the permutation , followed by the number ().
Write the permutation .
The expected solution to this problem has cost . The solutions that have cost can get at most points over .
You can add (few) lines of comments explaining what you intend to do.
If needed, you can use that the product of permutations is associative.
Input
3 1 2 0 0 3 1 2 0 2 4 0 2 3 1 1 10 4 3 7 8 0 5 2 1 6 9 5
Output
0 1 2 2 0 1 0 2 3 1 4 7 6 1 0 5 8 2 3 9