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 Haskell (GHC 7.10.3) (judge verdict: Accepted)

{-# LANGUAGE
  ScopedTypeVariables, BangPatterns, TupleSections, ExplicitForAll,
  LambdaCase, MultiWayIf, Unsafe, RecordWildCards, FlexibleContexts, CPP,
  NoMonomorphismRestriction, GADTs, ImplicitParams,
  RankNTypes, EmptyDataDecls, EmptyCase, ViewPatterns #-}


import Data.Bits
import Data.List
import Data.Maybe
import Data.Tuple
import Data.Ord
import Data.Int
import Data.Word
import Data.Char
import Data.Function
import Data.STRef
import Data.IORef
import Data.Monoid
import qualified Data.Foldable as F
import System.IO
import Control.Applicative
import Control.Monad
import Control.Monad.Primitive
import Control.Monad.State.Strict
import Control.Monad.ST
-- import Control.Monad.ST.Safe
import Control.DeepSeq
import qualified Data.ByteString.Char8 as BS
import qualified Data.ByteString.Builder as BSB
import qualified Data.ByteString.Lazy.Char8 as BSL
import Data.IntMap (IntMap)
import qualified Data.IntMap.Strict as IMS
import qualified Data.IntMap.Lazy as IML
import Data.IntSet (IntSet)
import qualified Data.IntSet as IS
import qualified Data.Sequence as Seq
import Data.Sequence (Seq)
import qualified Data.Array.IArray as A
import qualified Data.Array.MArray.Safe as A
import qualified Data.Array.MArray as A
import Data.Array (Array)
import Data.Array.Unboxed (UArray)
import Data.Array.IArray (IArray)
import Data.Array.MArray.Safe (MArray)
import Data.Array.IO.Safe (IOArray, IOUArray)
import Data.Array.ST.Safe (STArray, STUArray, runSTArray, runSTUArray)
import qualified Data.Vector as V
import qualified Data.Vector.Mutable as VM
import qualified Data.Vector.Unboxed as VU
import qualified Data.Vector.Unboxed.Mutable as VUM
import qualified Data.Vector.Primitive as VP
import qualified Data.Vector.Primitive.Mutable as VPM
import qualified Data.Vector.Generic as VG
import qualified Data.Vector.Generic.Mutable as VGM

-- import Control.Monad.ST.Unsafe

main :: IO ()
main = do
  [n,k] <- map readInt . words <$> getLine
  [p,q] <- replicateM 2 $ VU.map (subtract 1)
    . VU.unfoldrN n (runStateT $ rIntS) <$> BS.getLine
  let ?n = n
  let (kd6, km6) = (k-1) `divMod` 6
      actor_main = sympow (conjinv q p `comp` p) kd6
      ans = (!! km6)
        [conj actor_main p,
         conj actor_main q,
         conj actor_main (f p q),
         conjinv (actor_main `comp` q) p,
         conjinv (comp actor_main q `g` p) q,
         conj (comp actor_main q `g` comp q p) (g p q)]
  BSB.hPutBuilder stdout
    $ F.fold
    $ V.imap (\i -> if i > 0 then (BSB.char7 ' ' <>) else id)
    $ V.map BSB.intDec $ V.convert $ VU.map (+1) ans
   

-- Aut_Set({0,..,n-1})
-- To be used under the constraint (?n :: Int)
-- INVARIANT : VU.length x == ?n for x :: Perm
-- INVARIANT : VU.all (uncurry (==)) $ VU.indexed $ sort x for x :: Perm
type Perm = VU.Vector Int

idperm :: (?n :: Int) => Perm
{-# INLINE idperm #-}
idperm = VU.generate ?n id

inv :: (?n :: Int) => Perm -> Perm
{-# INLINE inv #-}
inv = g idperm

comp :: Perm -> Perm -> Perm
{-# INLINE comp #-}
comp = VU.map . (VU.!)

-- f p q = qp^{-1}
f :: (?n :: Int) => Perm -> Perm -> Perm
{-# INLINE f #-}
f = VU.update_ (VU.replicate ?n (-1))

-- g p q = pq^{-1}
g :: (?n :: Int) => Perm -> Perm -> Perm
{-# INLINE g #-}
g = flip f

-- conj p x = pxp^{-1}
conj :: (?n :: Int) => Perm -> Perm -> Perm
{-# INLINE conj #-}
conj p x = g (comp p x) p

-- conjinv p x = px^{-1}p^{-1}
conjinv :: (?n :: Int) => Perm -> Perm -> Perm
{-# INLINE conjinv #-}
conjinv p x = g p (comp p x)

sympow :: (?n :: Int) => Perm -> Int -> Perm
sympow p 0 = idperm
sympow p k = VU.create $ do
  vec <- VU.thaw idperm
  let loop !p !k = do
        when (testBit k 0) $ forM_ [0 .. ?n-1] $ VUM.modify vec (p VU.!)
        let k1 = shiftR k 1
        when (k1 > 0) $ loop (comp p p) k1
  if k >= 0 then loop p k else loop (inv p) (-k)
  return vec


rInt :: StateT BSL.ByteString Maybe Int
rInt = StateT $ BSL.readInt . BSL.dropWhile (<'!')
rIntS :: StateT BS.ByteString Maybe Int
rIntS = StateT $ BS.readInt . BS.dropWhile (<'!')

#define D(f,r,d) f::Integral a=>a->d;f=fromIntegral;r::String->d;r=read
#define C(f,r,g,h,d) D(f,r,d);g,h::RealFrac a=>a->d;g=floor;h=ceiling
C(_toInteger_,readInteger,floorInteger,ceilInteger,Integer)
C(toInt,readInt,floorInt,ceilInt,Int)
C(toI8,readI8,floorI8,ceilI8,Int8)
C(toI16,readI16,floorI16,ceilI16,Int16)
C(toI32,readI32,floorI32,ceilI32,Int32)
C(toI64,readI64,floorI64,ceilI64,Int64)
C(toWord,readWord,floorWord,ceilWord,Word)
C(toW8,readW8,floorW8,ceilW8,Word8)
C(toW16,readW16,floorW16,ceilW16,Word16)
C(toW32,readW32,floorW32,ceilW32,Word32)
C(toW64,readW64,floorW64,ceilW64,Word64)
D(toDouble,readDouble,Double)
D(toFloat,readFloat,Float)
#undef D
#undef C

{-# INLINE wrA #-}
wrA = A.writeArray
{-# INLINE rdA #-}
rdA = A.readArray
{-# INLINE mdA #-}
mdA arr f i = do
  ai <- rdA arr i
  let fai = f ai 
  wrA arr i fai
  return (ai,fai)
{-# INLINE mdA' #-}
mdA' arr f i = do
  !ai <- rdA arr i
  let !fai = f ai
  wrA arr i fai
  return (ai,fai)
{-# INLINE swapA #-}
swapA arr i j = do
  ai <- rdA arr i
  wrA arr i =<< rdA arr j
  wrA arr j ai

#define N(f,g,a,m)\
  f :: forall e i s. (C(a,m)A.Ix i) => (i,i) -> e -> m (a i e); f=A.newArray;\
  g :: forall e i s. (C(a,m)A.Ix i) => (i,i) -> m (a i e); g=A.newArray_
#define C(a,m)
N(newIOA,newIOA_,IOArray,IO)
N(newSTA,newSTA_,STArray s,ST s)
#undef C
#define C(a,m) MArray (a) e (m), 
N(newIOUA,newIOUA_,IOUArray,IO)
N(newSTUA,newSTUA_,STUArray s,ST s)
#undef C
#undef N