結果

問題 No.3 ビットすごろく
ユーザー taktak
提出日時 2019-04-27 22:37:19
言語 F#
(F# 4.0)
結果
AC  
実行時間 200 ms / 5,000 ms
コード長 1,077 bytes
コンパイル時間 4,501 ms
コンパイル使用メモリ 164,100 KB
実行使用メモリ 24,388 KB
最終ジャッジ日時 2023-09-14 01:17:53
合計ジャッジ時間 9,384 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
22,332 KB
testcase_01 AC 57 ms
20,252 KB
testcase_02 AC 58 ms
22,272 KB
testcase_03 AC 65 ms
24,388 KB
testcase_04 AC 57 ms
22,156 KB
testcase_05 AC 95 ms
24,304 KB
testcase_06 AC 65 ms
20,128 KB
testcase_07 AC 58 ms
24,272 KB
testcase_08 AC 79 ms
20,108 KB
testcase_09 AC 120 ms
24,372 KB
testcase_10 AC 146 ms
22,344 KB
testcase_11 AC 108 ms
24,328 KB
testcase_12 AC 94 ms
22,332 KB
testcase_13 AC 64 ms
24,340 KB
testcase_14 AC 147 ms
24,368 KB
testcase_15 AC 194 ms
22,304 KB
testcase_16 AC 175 ms
22,320 KB
testcase_17 AC 190 ms
22,276 KB
testcase_18 AC 61 ms
24,252 KB
testcase_19 AC 195 ms
22,336 KB
testcase_20 AC 56 ms
22,352 KB
testcase_21 AC 55 ms
22,288 KB
testcase_22 AC 147 ms
22,240 KB
testcase_23 AC 200 ms
22,460 KB
testcase_24 AC 198 ms
20,272 KB
testcase_25 AC 195 ms
24,376 KB
testcase_26 AC 56 ms
22,412 KB
testcase_27 AC 62 ms
24,320 KB
testcase_28 AC 170 ms
20,184 KB
testcase_29 AC 111 ms
22,244 KB
testcase_30 AC 54 ms
22,216 KB
testcase_31 AC 57 ms
22,288 KB
testcase_32 AC 102 ms
20,260 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) F# Compiler version 11.0.0.0 for F# 5.0
Copyright (c) Microsoft Corporation. All Rights Reserved.

ソースコード

diff #

open System

let bitCount x =
    let mutable x = x
    x <- x - ((x >>> 1) &&& 0x55555555)
    x <- (x &&& 0x33333333) + ((x >>> 2) &&& 0x33333333)
    x <- (x + (x >>> 4)) &&& 0x0f0f0f0f;
    x <- x + (x >>> 8);
    x <- x + (x >>> 16);
    x &&& 0x3f

let isLeachable n =
    let dis = Array.zeroCreate (n + 1)
    dis.[1] <- 1
    let rec visit x =        
        let bit = bitCount x
        let f = x - bit
        let b = x + bit
        let newCost = dis.[x] + 1
        let f1 = f >= 1 && (dis.[f] = 0 || dis.[f] > newCost)
        let f2 = b <= n && (dis.[b] = 0 || dis.[b] > newCost)
        match (f1, f2) with
        | true, true -> 
            dis.[f] <- newCost
            dis.[b] <- newCost
            visit f
            visit b
        | true, false ->
            dis.[f] <- newCost
            visit f
        | false, true ->
            dis.[b] <- newCost
            visit b
        | false, false ->
            ()
    visit 1
    dis.[n]
        
let N = Console.ReadLine() |> int

isLeachable N
|> function
| 0 -> -1
| x -> x
|> Console.WriteLine
0