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)

// https://atcoder.jp/contests/agc031/tasks/agc031_d
//
#![allow(unused_imports)]
use std::cmp::*;
use std::collections::*;
use std::fmt::*;
use std::io::*;
use std::str::*;

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, 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! debug {
    ($($a:expr),*) => {
        println!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
    }
}

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

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

fn pow(p: &Vec<usize>, k: u64) -> Vec<usize> {
    let n = p.len();
    let mut r = vec![0; n];
    for i in 0..n {
        r[i] = i;
    }
    let mut pp = p.clone();
    let mut k = k;
    while k >= 1 {
        if k & 1 == 1 {
            r = mul(&r, &pp);
        }
        pp = mul(&pp, &pp);
        k >>= 1;
    }
    r
}

fn solve_small(p0: Vec<usize>, q0: Vec<usize>, k: u64) -> Vec<usize> {
    if k == 1 {
        return p0;
    }
    if k == 2 {
        return q0;
    }
    let mut p = p0;
    let mut q = q0;
    for _i in 3..k + 1 {
        let rev = inv(&p);
        let neq = mul(&rev, &q);
        p = q.clone();
        q = neq;
    }
    q
}

fn solve(p0: Vec<usize>, q0: Vec<usize>, k: u64) -> Vec<usize> {
    if k <= 20 {
        return solve_small(p0, q0, k);
    }
    let p1 = inv(&p0);
    let q1 = inv(&q0);

    let prefix = mul(&mul(&mul(&q1, &p0), &q0), &p1);
    let suffix = mul(&mul(&mul(&p0, &q1), &p1), &q0);

    let prcount = (k - 2) / 6;
    let sucount = (k + 1) / 6;
    let center = (k - 2) % 6;

    let p = pow(&prefix, prcount);
    let s = pow(&suffix, sucount);
    let c = match center {
        0 => q0.clone(),
        1 => mul(&p1, &q0),
        2 => mul(&mul(&q1, &p1), &q0),
        3 => q1.clone(),
        4 => mul(&q1, &p0),
        5 => mul(&mul(&q1, &p0), &q0),
        _ => panic!("no"),
    };
    mul(&mul(&p, &c), &s)
}

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

    let ans = solve(p, q, k);

    for i in 0..n {
        if i >= 1 {
            print!(" ");
        }
        print!("{}", ans[i] + 1);
    }
    println!();
}