結果
問題 |
No.3 ビットすごろく
|
ユーザー |
|
提出日時 | 2019-07-08 22:29:30 |
言語 | Ruby (3.4.1) |
結果 |
AC
|
実行時間 | 96 ms / 5,000 ms |
コード長 | 625 bytes |
コンパイル時間 | 90 ms |
コンパイル使用メモリ | 7,680 KB |
実行使用メモリ | 12,288 KB |
最終ジャッジ日時 | 2024-07-01 09:24:15 |
合計ジャッジ時間 | 4,289 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 33 |
コンパイルメッセージ
Syntax OK
ソースコード
def bit_count(n) cnt = 0 while 0 < n n &= n-1 cnt += 1 end cnt end def main n = gets.to_i dp = Array.new(n + 1, -1) dp[0] = 0 dp[1] = 1 candidates = [1] while !candidates.empty? candidate = candidates.shift break if candidate == n step = bit_count(candidate) if step < candidate && dp[candidate - step] < 0 dp[candidate - step] = dp[candidate] + 1 candidates << candidate - step end if step + candidate <= n && dp[candidate + step] < 0 dp[candidate + step] = dp[candidate] + 1 candidates << candidate + step end end dp[n] end puts main