Powers of permutations X39049


Statement
 

pdf   zip

html

Given an n, a permutation of {0, 1, … n−1} is a sequence where each of the numbers 0, 1, … n−1 occurs exactly once. For example, if n = 3, the sequences (1 2 0), (2 0 1) and (0 1 2) are permutations of {0, 1, 2}.

Given two permutations σ = (σ0, …, σn−1) and τ = (τ0, …, τn−1) of {0, 1, … n−1 }, their product σ ∘ τ is defined as the permutation ρ = (ρ0, …, ρn−1) such that ρi = στi. For example, if n = 3, σ = (1 2 0) and τ = (2 0 1), then σ ∘ τ = (0 1 2), since:

  • τ0 = 2 and σ2 = 0,
  • τ1 = 0 and σ0 = 1, and
  • τ2 = 1 and σ1 = 2.

Make a program that, given a permutation σ and a natural k, computes the power of σ raised to k: σk = σ ∘ … ∘ σk). By convention, σ0 = (0, 1, …, n−1).

Input

The input includes several cases. Each case consists in the number n (1 ≤ n ≤ 104), followed by n numbers between 1 and n that describe the permutation σ, followed by the number k (0 ≤ k ≤ 109).

Output

Write the permutation σk.

Observation

The expected solution to this problem has cost O(n · logk). The solutions that have cost Ω(n · k) can get at most 3 points over 10.


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.

Public test cases
  • 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
    
  • Information
    Author
    Enric Rodríguez
    Language
    English
    Translator
    Original language
    Catalan
    Other languages
    Catalan
    Official solutions
    C++
    User solutions
    C++