結果

問題 No.3 ビットすごろく
ユーザー mtwtkmanmtwtkman
提出日時 2023-05-16 19:40:33
言語 Haskell
(9.8.2)
結果
AC  
実行時間 332 ms / 5,000 ms
コード長 1,061 bytes
コンパイル時間 3,600 ms
コンパイル使用メモリ 163,104 KB
実行使用メモリ 11,580 KB
最終ジャッジ日時 2023-08-21 08:23:13
合計ジャッジ時間 9,594 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,976 KB
testcase_01 AC 3 ms
6,988 KB
testcase_02 AC 3 ms
6,988 KB
testcase_03 AC 32 ms
11,268 KB
testcase_04 AC 8 ms
9,792 KB
testcase_05 AC 112 ms
11,364 KB
testcase_06 AC 42 ms
11,252 KB
testcase_07 AC 22 ms
11,124 KB
testcase_08 AC 73 ms
11,288 KB
testcase_09 AC 162 ms
11,424 KB
testcase_10 AC 232 ms
11,480 KB
testcase_11 AC 142 ms
11,376 KB
testcase_12 AC 102 ms
11,436 KB
testcase_13 AC 32 ms
11,164 KB
testcase_14 AC 222 ms
11,452 KB
testcase_15 AC 322 ms
11,500 KB
testcase_16 AC 283 ms
11,528 KB
testcase_17 AC 312 ms
11,492 KB
testcase_18 AC 22 ms
11,256 KB
testcase_19 AC 332 ms
11,528 KB
testcase_20 AC 5 ms
8,748 KB
testcase_21 AC 2 ms
6,892 KB
testcase_22 AC 222 ms
11,456 KB
testcase_23 AC 332 ms
11,504 KB
testcase_24 AC 332 ms
11,580 KB
testcase_25 AC 322 ms
11,568 KB
testcase_26 AC 3 ms
6,992 KB
testcase_27 AC 32 ms
11,216 KB
testcase_28 AC 272 ms
11,520 KB
testcase_29 AC 152 ms
11,336 KB
testcase_30 AC 3 ms
7,288 KB
testcase_31 AC 3 ms
7,356 KB
testcase_32 AC 132 ms
11,384 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
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 #

split :: String -> [String]
split = go ("", [])
  where
    go :: (String, [String]) -> String -> [String]
    go (a, acc) "" = acc <> [a]
    go (a, acc) (' ' : xs) = go ("", acc <> [a]) xs
    go (a, acc) (x : xs) = go (a <> [x], acc) xs

prompt :: IO [String]
prompt = do split <$> getLine

bin :: Int -> [Int]
bin x
  | x <= 1 = [x]
  | otherwise =
      let (n, r) = x `quotRem` 2
       in bin n <> [r]

step :: Int -> Int
step = sum . bin

solve :: Int -> Int
solve n = go [(1, 1)] []
  where
    go :: [(Int, Int)] -> [Int] -> Int
    go [] _ = -1
    go ((p, c) : qs) visited =
      if p == n
        then c
        else
          let prev = p - step p
              next = p + step p
              canPrev = prev > 0 && notElem prev visited
              canNext = next <= n && notElem next visited
              q = [(prev, c + 1) | canPrev] <> [(next, c + 1) | canNext]
              v' = [prev | canPrev] <> [next | canNext]
           in go (qs <> q) (v' <> visited)

main :: IO ()
main = do
  n :: Int <- read . head <$> prompt
  print $ solve n
0