結果

問題 No.3 ビットすごろく
ユーザー pocaristpocarist
提出日時 2015-09-01 15:21:43
言語 OCaml
(5.1.0)
結果
AC  
実行時間 2,521 ms / 5,000 ms
コード長 954 bytes
コンパイル時間 327 ms
コンパイル使用メモリ 21,540 KB
実行使用メモリ 45,312 KB
最終ジャッジ日時 2024-04-17 08:30:57
合計ジャッジ時間 20,855 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 5 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 47 ms
9,856 KB
testcase_06 AC 5 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 18 ms
9,856 KB
testcase_09 AC 332 ms
10,368 KB
testcase_10 AC 972 ms
16,412 KB
testcase_11 AC 199 ms
10,240 KB
testcase_12 AC 39 ms
9,856 KB
testcase_13 AC 4 ms
5,376 KB
testcase_14 AC 799 ms
16,256 KB
testcase_15 AC 2,148 ms
37,464 KB
testcase_16 AC 1,178 ms
16,252 KB
testcase_17 AC 1,875 ms
30,992 KB
testcase_18 AC 3 ms
5,376 KB
testcase_19 AC 2,521 ms
45,144 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 853 ms
16,260 KB
testcase_23 AC 2,503 ms
45,312 KB
testcase_24 AC 2,481 ms
45,128 KB
testcase_25 AC 2,131 ms
37,464 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 5 ms
5,376 KB
testcase_28 AC 1,116 ms
16,260 KB
testcase_29 AC 205 ms
10,112 KB
testcase_30 AC 2 ms
5,376 KB
testcase_31 AC 2 ms
5,376 KB
testcase_32 AC 119 ms
10,112 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

(* http://yukicoder.me/problems/11 *)

let bitcount n =
    let rec loop bits num =
        if bits = 0 then num
        else loop (bits land (bits-1)) (num+1)
    in
    loop n 0

let solve n =
    let visited = Array.init (n+1) (fun _ -> false) in
    let q = Queue.create () in
    Queue.add (1,1) q;
    let ans = ref 0 in
    while !ans = 0 do
        if Queue.is_empty q then
            ans := -1
        else
        let i, cnt = Queue.take q in
        if i = n then ans := cnt
        else (
            visited.(i) <- true;
            let num = bitcount i in
            if i+num = n then ans := cnt+1
            else (
                if i+num < n && not visited.(i+num) then
                    Queue.add (i+num, cnt+1) q;
                if i-num >= 1 && not visited.(i-num) then
                    Queue.add (i-num, cnt+1) q;
            )
        )
    done;
    !ans

let () =
    Scanf.scanf "%d\n" solve
    |> Printf.printf "%d\n"
0