結果

問題 No.26 シャッフルゲーム
ユーザー tottoripaper
提出日時 2014-11-16 20:57:48
言語 Haskell
(9.10.1)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 601 bytes
コンパイル時間 6,140 ms
コンパイル使用メモリ 175,744 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-12-31 21:11:01
合計ジャッジ時間 6,774 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 10
権限があれば一括ダウンロードができます
コンパイルメッセージ
Loaded package environment from /home/judge/.ghc/x86_64-linux-9.8.2/environments/default
[1 of 2] Compiling Main             ( Main.hs, Main.o )
[2 of 2] Linking a.out

ソースコード

diff #

import Control.Applicative
import Control.Monad
import Data.Bits

readInts :: IO [Int]
readInts = map read . words <$> getLine

rightmost :: Int -> Int
rightmost n = length $ takeWhile (not . testBit n) [0..31]

swap :: Int -> Int -> Int -> Int
swap x n m = x `xor` a `xor` shift a d
  where
    d = m-n
    a = (shift x (-d) `xor` x) .&. shift 1 n

main = do
  n <- readLn
  m <- readLn
  query <- replicateM m readInts
  let
    query' = map (map (subtract 1)) query
    f [] s = 1 + rightmost s
    f ([p,q]:rs) s = f rs (swap s p q)
   in print $ f query' (shift 1 (n-1))
0