{-# LANGUAGE BangPatterns #-} import Data.Bits import qualified Data.Vector.Fusion.Stream.Monadic as VFSM import qualified Data.Vector.Unboxed.Mutable as VUM main :: IO () main = do [n, x', y', z'] <- map (read :: String -> Int) . words <$> getLine let x = x' - 1 y = y' - 1 z = z' - 1 as <- VUM.unsafeNew 256 :: IO (VUM.IOVector Int) rep n $ \i -> rep n $ \j -> do let k = i * n + j item = if (((j + 1) .^. (i + 1)) .&. 2) /= 0 then n * n - 1 - k else k VUM.unsafeWrite as (i * 16 + j) item rep n $ \i -> rep n $ \j -> do let sep = if j `mod` n == (n - 1) then "\n" else " " item1 <- VUM.unsafeRead as (i * 16 + j) item2 <- VUM.unsafeRead as (y * 16 + x) putStr . (++ sep) . show $ (item1 .^. item2 .^. z) + 1 infixl 6 .^. (.^.) :: Bits b => b -> b -> b (.^.) = xor {-# INLINE (.^.) #-} stream :: Monad m => Int -> Int -> VFSM.Stream m Int stream !l !r = VFSM.Stream step l where step x | x < r = return $ VFSM.Yield x (x + 1) | otherwise = return $ VFSM.Done {-# INLINE [0] step #-} {-# INLINE [1] stream #-} rep :: Monad m => Int -> (Int -> m ()) -> m () rep n = flip VFSM.mapM_ (stream 0 n) {-# INLINE rep #-}