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)

// ${url}
//
#![allow(unused_imports)]
use std::io::*;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

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

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

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

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

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

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

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

#[allow(unused_macros)]
macro_rules! dvec {
    ($t:expr ; $len:expr) => {
        vec![$t; $len]
    };

    ($t:expr ; $len:expr, $($rest:expr),*) => {
        vec![dvec!($t; $($rest),*); $len]
    };
}

#[allow(unused_macros)]
macro_rules! ifv {
    ($t:expr, $a:expr, $b: expr) => {
        if $t { $a } else { $b }
    }
}

#[allow(unused_macros)]
macro_rules! fill {
    ($t:expr, $v:expr) => {
        for i in 0..$t.len() {
            $t[i] = $v;
        }
    };
}

#[allow(unused_macros)]
macro_rules! join {
    ($t:expr, $glue:expr) => {
        $t.into_iter().map(|w| w.to_string()).collect::<Vec<_>>().join($glue)
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        println!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
    }
}

fn next(a: &Vec<usize>, b: &Vec<usize>) -> Vec<usize> {
    let mut c = a.clone();
    c.reverse();
    let al = a.len();
    for i in 0..al {
        c[i] ^= 1;
    }

    let bn = b.len();
    for i in 0..bn {
        let n = c.len();
        if c[n-1] ^ 1 == b[i] {
            c.pop();
        } else {
            c.push(b[i]);
        }
    }
    c
}

fn combine(a: &Vec<usize>, b: &Vec<usize>) -> Vec<usize> {
    let n = a.len();
    let mut c = vec![0; n];
    for i in 0..n {
        c[i] = b[a[i]];
    }
    c
}

fn pow(a: &Vec<usize>, mut p: i64) -> Vec<usize> {
    let n = a.len();
    let mut aa = a.clone();
    let mut ret = vec![0; n];
    for i in 0..n {
        ret[i] = i;
    }
    while p >= 1 {
        if p & 1 == 1 {
            ret = combine(&ret, &aa);
        }
        aa = combine(&aa, &aa);
        p >>= 1;
    }
    ret
}

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

fn wl(a: &Vec<usize>, elements: &Vec<Vec<usize>>) -> Vec<usize> {
    let n = elements[0].len();
    let mut ret = vec![0; n];
    for i in 0..n {
        ret[i] = i;
    }
    for ai in a {
        ret = combine(&ret, &elements[*ai]);
    }
    ret
}

fn main() {
    input! {
        n: usize, k: usize,
        p: [usize1; n],
        q: [usize1; n]
    };

    let ip = inv(&p);
    let iq = inv(&q);

    let elements = vec![
        p.clone(),
        ip.clone(),
        q.clone(),
        iq.clone()
    ];

    let mut w = vec![];
    w.push(vec![]);
    w.push(vec![0]);
    w.push(vec![2]);
    for i in 3..20 {
        let z = next(&w[i-2], &w[i-1]);
        w.push(z.clone());
    }

    // a: [3, 0, 2, 1] i=8,14..
    // b: [0, 3, 1, 2] i=5,11,17

    let pa = wl(&vec![3, 0, 2, 1], &elements);
    let pb = wl(&vec![0, 3, 1, 2], &elements);

    let tbl = vec![
        vec![3, 0],
        vec![3, 0, 2],
        vec![2],
        vec![1, 2],
        vec![3, 1, 2],
        vec![3],
    ];

    let mut ans = vec![0; n];
    for i in 0..n {
        ans[i] = i;
    }
    if k <= 10 {
        let ln = w[k].len();
        for i in 0..ln {
            ans = combine(&ans, &elements[w[k][i]]);
        }
    } else {
        let cnt_a = (k-8)/6+1;
        ans = combine(&ans, &pow(&pa, cnt_a as i64));
        let md = k % 6;
        let p = wl(&tbl[md], &elements);
        ans = combine(&ans, &p);
        let cnt_b = (k-5)/6+1;
        ans = combine(&ans, &pow(&pb, cnt_b as i64));
    }
    for i in 0..n {
        ans[i] += 1;
    }
    println!("{}", join!(ans, " "));
}