結果
| 問題 |
No.3 ビットすごろく
|
| コンテスト | |
| ユーザー |
pocarist
|
| 提出日時 | 2015-09-01 15:21:43 |
| 言語 | OCaml (5.2.1) |
| 結果 |
AC
|
| 実行時間 | 2,502 ms / 5,000 ms |
| コード長 | 954 bytes |
| コンパイル時間 | 481 ms |
| コンパイル使用メモリ | 21,668 KB |
| 実行使用メモリ | 45,528 KB |
| 最終ジャッジ日時 | 2024-10-08 23:39:07 |
| 合計ジャッジ時間 | 21,463 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 33 |
ソースコード
(* 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"
pocarist