結果
問題 | No.3 ビットすごろく |
ユーザー |
|
提出日時 | 2019-07-19 12:02:05 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
AC
|
実行時間 | 41 ms / 5,000 ms |
コード長 | 735 bytes |
コンパイル時間 | 94 ms |
コンパイル使用メモリ | 12,544 KB |
実行使用メモリ | 11,776 KB |
最終ジャッジ日時 | 2024-07-01 09:26:03 |
合計ジャッジ時間 | 2,318 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 33 |
ソースコード
from collections import dequedef bit_count(n):cnt = 0while 0 < n:n &= n-1cnt += 1return cntdef main():""" main """N = int(input())dp = set()dp.add(1)q = deque([(1, 1)])# queueが空になるまでloopを回し、到達すればbreak and returnwhile q:pos, cnt = q.popleft()if pos == N:print(cnt)breakstep = bit_count(pos)if 1 < (pos - step) and pos-step not in dp:dp.add(pos-step)q.append((pos-step, cnt+1))if (pos + step) <= N and pos + step not in dp:dp.add(pos+step)q.append((pos+step, cnt+1))else:print(-1)main()