Problem:
Score : 1000 points

Problem Statement

For two permutations p and q of the integers from 1 through N, let f(p,q) be the permutation that satisfies the following:

The p_i-th element (1 \leq i \leq N) in f(p,q) is q_i.
Here, p_i and q_i respectively denote the i-th element in p and q.

You are given two permutations p and q of the integers from 1 through N.
We will now define a sequence {a_n} of permutations of the integers from 1 through N, as follows:

a_1=p, a_2=q

a_{n+2}=f(a_n,a_{n+1}) ( n \geq 1 )

Given a positive integer K, find a_K.

Constraints

1 \leq N \leq 10^5

1 \leq K \leq 10^9

p and q are permutations of the integers from 1 through N.

Input

Input is given from Standard Input in the following format:

N K
p_1 ... p_N
q_1 ... q_N

Output

Print N integers, with spaces in between.
The i-th integer (1 \leq i \leq N) should be the i-th element in a_K.

Sample Input 1

3 3
1 2 3
3 2 1

Sample Output 1

3 2 1

Since a_3=f(p,q), we just need to find f(p,q).
We have p_i=i here, so f(p,q)=q.

Sample Input 2

5 5
4 5 1 2 3
3 2 1 5 4

Sample Output 2

4 3 2 1 5

Sample Input 3

10 1000000000
7 10 6 5 4 2 9 1 3 8
4 1 9 2 3 7 8 10 6 5

Sample Output 3

7 9 4 8 2 5 1 6 10 3

Solution in Rust (1.15.1) (judge verdict: Accepted)

#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };

    ($next:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, bytes) => {
        read_value!($next, String).into_bytes()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

fn main() {
    // let mut a = vec![0];
    // let mut b = vec![1];

    // for i in 0..100 {
    //     println!("{}: {:?}", i, a);

    //     let mut c = b.clone();
    //     for &x in a.iter().rev() {
    //         if c.last().cloned() == Some(x) {
    //             c.pop();
    //         } else {
    //             c.push(x ^ 2);
    //         }
    //     }

    //     a = b;
    //     b = c;
    // }
    // return;

    input! {
        n: usize,
        k: usize1,
        p: [usize1; n],
        q: [usize1; n],
    }

    // 6:  [1, 2, 3, 0, 1, 0, 3]
    // 7:  [1, 2, 3, 0, 1, 2, 1, 0, 3]
    // 8:  [1, 2, 3, 0, 1, 2, 2, 1, 0, 3]
    // 9:  [1, 2, 3, 0, 1, 2, 3, 2, 1, 0, 3]
    // 10: [1, 2, 3, 0, 1, 2, 3, 0, 3, 2, 1, 0, 3]
    // 11: [1, 2, 3, 0, 1, 2, 3, 0, 0, 3, 2, 1, 0, 3]

    // 12: [1, 2, 3, 0, 1, 2, 3, 0, 1, 0, 3, 2, 1, 0, 3]
    // 13: [1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 1, 0, 3, 2, 1, 0, 3]
    // 14: [1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 2, 1, 0, 3, 2, 1, 0, 3]
    // 15: [1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 2, 1, 0, 3, 2, 1, 0, 3]
    // 16: [1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 3, 2, 1, 0, 3, 2, 1, 0, 3]
    // 17: [1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 0, 3, 2, 1, 0, 3, 2, 1, 0, 3]

    let mut a = vec![vec![0; n]; 4];
    for i in 0..n {
        a[0][i] = p[i];
    }
    for i in 0..n {
        a[1][i] = q[i];
    }
    a[2] = inv(&a[0]);
    a[3] = inv(&a[1]);

    let m = if k < 6 {
        let tbl: &[&[usize]] = &[
            &[0],
            &[1],
            &[1, 2],
            &[1, 2, 3],
            &[1, 2, 3, 0, 3],
            &[1, 2, 3, 0, 0, 3],
        ];

        let mut m = id(n);
        for &aix in tbl[k].iter() {
            m = mulv(&m, &a[aix]);
        }
        m
    } else {
        let tbl: &[&[usize]] = &[
            &[1, 0, 3],
            &[1, 2, 1, 0, 3],
            &[1, 2, 2, 1, 0, 3],
            &[1, 2, 3, 2, 1, 0, 3],
            &[1, 2, 3, 0, 3, 2, 1, 0, 3],
            &[1, 2, 3, 0, 0, 3, 2, 1, 0, 3],
        ];

        let t = mulv(&a[1], &mulv(&a[2], &mulv(&a[3], &a[0])));
        let u = mulv(&a[2], &mulv(&a[1], &mulv(&a[0], &a[3])));

        let mut m = powv(&t, k / 6);
        for &aix in tbl[k % 6].iter() {
            m = mulv(&m, &a[aix]);
        }
        m = mulv(&m, &powv(&u, k / 6 - 1));
        m
    };

    // dbg!(&m);

    for (i, x) in m.into_iter().enumerate() {
        if i > 0 {
            print!(" ");
        }
        print!("{}", x + 1);
    }
    println!();
}

fn id(n: usize) -> Vec<usize> {
    (0..n).collect()
}

fn mulv(p: &Vec<usize>, q: &Vec<usize>) -> Vec<usize> {
    let mut v = vec![0; p.len()];
    for i in 0..p.len() {
        v[i] = p[q[i]];
    }
    v
}

fn inv(v: &Vec<usize>) -> Vec<usize> {
    let mut w = vec![0; v.len()];
    for i in 0..v.len() {
        w[v[i]] = i;
    }
    w
}

fn powv(v: &Vec<usize>, n: usize) -> Vec<usize> {
    if n == 0 {
        return id(v.len());
    } else if n == 1 {
        return v.clone();
    }

    let m = n / 2;
    let mut w = powv(&mulv(v, v), m);
    if n % 2 == 1 {
        w = mulv(&w, v);
    }
    w
}