結果

問題 No.3 ビットすごろく
ユーザー shinshin
提出日時 2022-10-10 16:27:57
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 25 ms / 5,000 ms
コード長 596 bytes
コンパイル時間 165 ms
コンパイル使用メモリ 10,940 KB
実行使用メモリ 8,480 KB
最終ジャッジ日時 2023-09-06 16:11:33
合計ジャッジ時間 2,221 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 14 ms
7,736 KB
testcase_01 AC 14 ms
7,828 KB
testcase_02 AC 14 ms
7,772 KB
testcase_03 AC 19 ms
7,820 KB
testcase_04 AC 17 ms
7,716 KB
testcase_05 AC 21 ms
8,288 KB
testcase_06 AC 18 ms
7,840 KB
testcase_07 AC 16 ms
7,732 KB
testcase_08 AC 19 ms
7,820 KB
testcase_09 AC 21 ms
8,272 KB
testcase_10 AC 23 ms
8,328 KB
testcase_11 AC 21 ms
8,248 KB
testcase_12 AC 20 ms
8,300 KB
testcase_13 AC 17 ms
7,788 KB
testcase_14 AC 23 ms
8,408 KB
testcase_15 AC 24 ms
8,376 KB
testcase_16 AC 24 ms
8,340 KB
testcase_17 AC 25 ms
8,404 KB
testcase_18 AC 18 ms
7,788 KB
testcase_19 AC 25 ms
8,416 KB
testcase_20 AC 14 ms
7,872 KB
testcase_21 AC 15 ms
7,832 KB
testcase_22 AC 23 ms
8,320 KB
testcase_23 AC 24 ms
8,480 KB
testcase_24 AC 25 ms
8,464 KB
testcase_25 AC 25 ms
8,412 KB
testcase_26 AC 14 ms
7,820 KB
testcase_27 AC 16 ms
7,780 KB
testcase_28 AC 24 ms
8,336 KB
testcase_29 AC 21 ms
8,136 KB
testcase_30 AC 14 ms
7,752 KB
testcase_31 AC 14 ms
7,872 KB
testcase_32 AC 20 ms
8,304 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#ビットすごろく

N = int(input())

root = list(0 for i in range(N+1))

root[1] = 1
stack = [1]

while len(stack) > 0:
    s = stack.pop(0)
    a = s + s.bit_count()
    b = s - s.bit_count()
    #print(a , b)
    
    
    if a <= N and (root[a] == 0 or root[a] > root[s] + 1):
        root[a] = root[s] + 1
        stack.append(a)
    
    if b == 0:
        #print(root , stack)
        continue
    elif root[b] == 0 or root[b] > root[s] + 1:
        root[b] = root[s] + 1
        stack.append(b)
    
    #print(root , stack)

if root[N] == 0:
        root[N] = -1

print(root[N])
    
0