結果

問題 No.3 ビットすごろく
ユーザー Ryushi-techRyushi-tech
提出日時 2020-06-22 18:22:38
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 36 ms / 5,000 ms
コード長 633 bytes
コンパイル時間 123 ms
コンパイル使用メモリ 10,924 KB
実行使用メモリ 10,340 KB
最終ジャッジ日時 2023-09-14 01:46:54
合計ジャッジ時間 2,246 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,620 KB
testcase_01 AC 18 ms
8,608 KB
testcase_02 AC 19 ms
8,516 KB
testcase_03 AC 22 ms
9,004 KB
testcase_04 AC 20 ms
8,616 KB
testcase_05 AC 27 ms
9,392 KB
testcase_06 AC 23 ms
8,936 KB
testcase_07 AC 21 ms
8,808 KB
testcase_08 AC 26 ms
9,212 KB
testcase_09 AC 30 ms
9,636 KB
testcase_10 AC 32 ms
9,936 KB
testcase_11 AC 30 ms
9,588 KB
testcase_12 AC 28 ms
9,424 KB
testcase_13 AC 25 ms
8,892 KB
testcase_14 AC 31 ms
9,824 KB
testcase_15 AC 35 ms
10,228 KB
testcase_16 AC 34 ms
10,096 KB
testcase_17 AC 36 ms
10,200 KB
testcase_18 AC 22 ms
8,736 KB
testcase_19 AC 36 ms
10,284 KB
testcase_20 AC 21 ms
8,552 KB
testcase_21 AC 19 ms
8,540 KB
testcase_22 AC 32 ms
9,876 KB
testcase_23 AC 34 ms
10,340 KB
testcase_24 AC 36 ms
10,160 KB
testcase_25 AC 35 ms
10,172 KB
testcase_26 AC 19 ms
8,536 KB
testcase_27 AC 23 ms
8,860 KB
testcase_28 AC 35 ms
10,032 KB
testcase_29 AC 29 ms
9,724 KB
testcase_30 AC 20 ms
8,540 KB
testcase_31 AC 20 ms
8,608 KB
testcase_32 AC 28 ms
9,512 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

n = int(input())
l = n.bit_length()
g = [[] for _ in range(n + 1 + l)]

if n == 1:
    print(1)
    exit()

for i in range(n + 1):
    x = bin(i).count("1")
    g[i].append(i + x)
    g[i].append(i - x)

par = [0] * (n + 1 + l)
chk = [False] * (n + 1 + l)
chk[0] = True
stk = deque()
stk.append(1)

while stk:
    v = stk.popleft()
    for y in g[v]:
        if chk[y]:
            continue
        if y == par[v]:
            continue
        chk[y] = True
        par[y] = v
        stk.append(y)

if not chk[n]:
    print(-1)
    exit()

cnt = 1
while n > 1:
    n = par[n]
    cnt += 1
print(cnt)

0