結果

問題 No.3 ビットすごろく
ユーザー sepa38sepa38
提出日時 2023-04-23 21:05:45
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 61 ms / 5,000 ms
コード長 382 bytes
コンパイル時間 348 ms
コンパイル使用メモリ 82,152 KB
実行使用メモリ 72,264 KB
最終ジャッジ日時 2024-04-25 15:43:46
合計ジャッジ時間 2,986 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
54,844 KB
testcase_01 AC 38 ms
53,924 KB
testcase_02 AC 37 ms
53,916 KB
testcase_03 AC 46 ms
64,644 KB
testcase_04 AC 40 ms
54,996 KB
testcase_05 AC 55 ms
67,664 KB
testcase_06 AC 47 ms
64,484 KB
testcase_07 AC 50 ms
63,800 KB
testcase_08 AC 51 ms
67,060 KB
testcase_09 AC 52 ms
69,056 KB
testcase_10 AC 53 ms
70,268 KB
testcase_11 AC 52 ms
68,692 KB
testcase_12 AC 55 ms
67,564 KB
testcase_13 AC 48 ms
64,732 KB
testcase_14 AC 54 ms
70,140 KB
testcase_15 AC 58 ms
70,968 KB
testcase_16 AC 59 ms
70,644 KB
testcase_17 AC 59 ms
71,288 KB
testcase_18 AC 50 ms
64,008 KB
testcase_19 AC 60 ms
71,136 KB
testcase_20 AC 43 ms
54,936 KB
testcase_21 AC 40 ms
54,724 KB
testcase_22 AC 58 ms
69,792 KB
testcase_23 AC 59 ms
72,264 KB
testcase_24 AC 60 ms
72,064 KB
testcase_25 AC 59 ms
71,352 KB
testcase_26 AC 40 ms
54,896 KB
testcase_27 AC 50 ms
64,108 KB
testcase_28 AC 61 ms
70,580 KB
testcase_29 AC 57 ms
68,404 KB
testcase_30 AC 42 ms
54,432 KB
testcase_31 AC 43 ms
54,248 KB
testcase_32 AC 60 ms
68,688 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
n = int(input())
inf = 1 << 30
dist = [inf] * (n + 1)
dist[1] = 1
d = deque([1])
while d:
  u = d.popleft()
  x = bin(u).count("1")
  if u + x <= n and dist[u+x] > dist[u] + 1:
    dist[u+x] = dist[u] + 1
    d.append(u+x)
  if 0 < u - x and dist[u-x] > dist[u] + 1:
    dist[u-x] = dist[u] + 1
    d.append(u-x)
print(dist[n] if dist[n] < inf else -1)
0