結果

問題 No.3 ビットすごろく
ユーザー DialBird
提出日時 2018-05-09 10:43:01
言語 Ruby
(3.4.1)
結果
WA  
実行時間 -
コード長 459 bytes
コンパイル時間 91 ms
コンパイル使用メモリ 7,424 KB
実行使用メモリ 133,376 KB
最終ジャッジ日時 2024-06-28 02:39:05
合計ジャッジ時間 6,981 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other WA * 3 TLE * 1 -- * 29
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

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(10001, 0)
  dp[0] = 1
  queue = [1]
  while 0 < queue.length
    pos = queue.shift
    bc = bit_count(pos)
    [pos + bc, pos - bc].each do |next_pos|
      next if next_pos < 2 && 10000 < next_pos
      return dp[pos] + 1 if next_pos == n

      dp[next_pos] = dp[pos] + 1
      queue << next_pos
    end
  end
  -1
end

puts main
0