結果
問題 |
No.3 ビットすごろく
|
ユーザー |
![]() |
提出日時 | 2017-05-04 21:52:02 |
言語 | Haskell (9.10.1) |
結果 |
TLE
|
実行時間 | - |
コード長 | 1,219 bytes |
コンパイル時間 | 1,841 ms |
コンパイル使用メモリ | 173,696 KB |
実行使用メモリ | 13,056 KB |
最終ジャッジ日時 | 2024-09-14 07:16:38 |
合計ジャッジ時間 | 8,367 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 3 TLE * 1 -- * 29 |
コンパイルメッセージ
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
ソースコード
import Prelude import Debug.Trace (traceShowId) import Data.Maybe (fromMaybe) main :: IO () main = do n <- read <$> getLine print $ calc n calc :: Int -> Int calc n = fromMaybe (-1) $ calc' [1] where calc' :: [Int] -> Maybe Int calc' ss@(s:ss') | s < 1 = Nothing | s > n = Nothing | elem s ss' = Nothing | s == n = Just $ length ss | otherwise = let b = countBits s in minLength (calc' ((s - b):ss)) (calc' ((s + b):ss)) minLength :: Maybe Int -> Maybe Int -> Maybe Int minLength (Just a) (Just b) = Just $ min a b minLength a@(Just _) _ = a minLength _ b@(Just _) = b minLength _ _ = Nothing type Binary = [Bool] countBits :: Int -> Int countBits n = length $ filter id $ toBinary n toBinary :: Int -> Binary toBinary n = toBinary' n $ reverse $ takeWhile (<= n) $ iterate (* 2) 1 where toBinary' :: Int -> [Int] -> Binary toBinary' i [_] = if i == 1 then [True] else [False] toBinary' i (p:ps) = if i >= p then True : toBinary' (i - p) ps else False : toBinary' i ps