結果

問題 No.3 ビットすごろく
ユーザー mottodoramottodora
提出日時 2016-10-21 16:45:12
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 43 ms / 5,000 ms
コード長 704 bytes
コンパイル時間 81 ms
コンパイル使用メモリ 10,988 KB
実行使用メモリ 9,284 KB
最終ジャッジ日時 2023-09-14 00:09:55
合計ジャッジ時間 2,374 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 21 ms
8,500 KB
testcase_01 AC 20 ms
8,512 KB
testcase_02 AC 19 ms
8,496 KB
testcase_03 AC 24 ms
8,700 KB
testcase_04 AC 20 ms
8,512 KB
testcase_05 AC 30 ms
8,900 KB
testcase_06 AC 24 ms
8,700 KB
testcase_07 AC 22 ms
8,520 KB
testcase_08 AC 28 ms
8,968 KB
testcase_09 AC 34 ms
8,896 KB
testcase_10 AC 37 ms
9,116 KB
testcase_11 AC 32 ms
8,968 KB
testcase_12 AC 30 ms
8,896 KB
testcase_13 AC 23 ms
8,752 KB
testcase_14 AC 37 ms
9,176 KB
testcase_15 AC 41 ms
9,284 KB
testcase_16 AC 40 ms
9,228 KB
testcase_17 AC 42 ms
9,228 KB
testcase_18 AC 22 ms
8,668 KB
testcase_19 AC 43 ms
9,188 KB
testcase_20 AC 20 ms
8,512 KB
testcase_21 AC 19 ms
8,532 KB
testcase_22 AC 38 ms
9,220 KB
testcase_23 AC 42 ms
9,184 KB
testcase_24 AC 43 ms
9,260 KB
testcase_25 AC 42 ms
9,276 KB
testcase_26 AC 19 ms
8,500 KB
testcase_27 AC 23 ms
8,664 KB
testcase_28 AC 40 ms
9,192 KB
testcase_29 AC 34 ms
8,888 KB
testcase_30 AC 19 ms
8,456 KB
testcase_31 AC 19 ms
8,568 KB
testcase_32 AC 33 ms
8,972 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict
memo = {1:1}

def sumbinary(n):
    if n == 0:
        return 0
    elif n % 2 == 0:
        return sumbinary(n//2)
    else:
        return sumbinary(n//2) + 1

target = int(input())

Q = [(1, 1)]

while True:
    if len(Q) == 0:
        result = -1
        break
    else:
        now, walk = Q.pop(0)
        if now == target:
            result = walk
            break
        d = sumbinary(now)
        if 0 < now-d <= target and not(now-d in memo):
            memo[now-d] = walk+1
            Q.append((now-d, walk+1))

        if 0 < now+d <= target and not(now+d in memo):
            memo[now+d] = walk+1
            Q.append((now+d, walk+1))

print(result)
0