結果

問題 No.3 ビットすごろく
ユーザー yutasthyutasth
提出日時 2017-05-04 21:52:02
言語 Haskell
(9.8.2)
結果
TLE  
実行時間 -
コード長 1,219 bytes
コンパイル時間 1,653 ms
コンパイル使用メモリ 161,756 KB
実行使用メモリ 15,184 KB
最終ジャッジ日時 2023-10-12 08:10:11
合計ジャッジ時間 7,636 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,932 KB
testcase_01 AC 3 ms
6,964 KB
testcase_02 AC 3 ms
7,016 KB
testcase_03 TLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Loaded package environment from /home/judge/.ghc/x86_64-linux-9.6.1/environments/default
[1 of 2] Compiling Main             ( Main.hs, Main.o )
[2 of 2] Linking a.out

ソースコード

diff #

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
0