結果
問題 | No.3 ビットすごろく |
ユーザー |
|
提出日時 | 2019-08-02 14:07:48 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
AC
|
実行時間 | 56 ms / 5,000 ms |
コード長 | 890 bytes |
コンパイル時間 | 88 ms |
コンパイル使用メモリ | 12,800 KB |
実行使用メモリ | 11,264 KB |
最終ジャッジ日時 | 2024-07-01 09:27:06 |
合計ジャッジ時間 | 2,416 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 33 |
ソースコード
def main():ret = solve(int(input()))print(ret)def solve(n: int) -> int:impossible = 10 ** 7steps = [step(i) for i in range(n + 1)]memo = [impossible for _ in range(n + 1)]stack = [1]memo[1] = 1while stack:cur = stack.pop()back = cur - steps[cur]front = cur + steps[cur]if back < 1:passelif memo[back] > memo[cur] + 1:memo[back] = memo[cur] + 1stack.append(back)if front > n:passelif memo[front] > memo[cur] + 1:memo[front] = memo[cur] + 1stack.append(front)if memo[n] == impossible:return -1else:return memo[n]def step(n):count = 0while n > 0:if n % 2 != 0:count += 1n //= 2return countif __name__ == '__main__':main()