結果
問題 | No.3 ビットすごろく |
ユーザー | tsukasa |
提出日時 | 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 ** 7 steps = [step(i) for i in range(n + 1)] memo = [impossible for _ in range(n + 1)] stack = [1] memo[1] = 1 while stack: cur = stack.pop() back = cur - steps[cur] front = cur + steps[cur] if back < 1: pass elif memo[back] > memo[cur] + 1: memo[back] = memo[cur] + 1 stack.append(back) if front > n: pass elif memo[front] > memo[cur] + 1: memo[front] = memo[cur] + 1 stack.append(front) if memo[n] == impossible: return -1 else: return memo[n] def step(n): count = 0 while n > 0: if n % 2 != 0: count += 1 n //= 2 return count if __name__ == '__main__': main()