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