結果

問題 No.3 ビットすごろく
ユーザー shi-moshi-mo
提出日時 2016-03-27 23:03:21
言語 Ruby
(3.3.0)
結果
AC  
実行時間 133 ms / 5,000 ms
コード長 577 bytes
コンパイル時間 231 ms
コンパイル使用メモリ 11,264 KB
実行使用メモリ 15,848 KB
最終ジャッジ日時 2023-09-13 23:48:11
合計ジャッジ時間 5,155 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 83 ms
15,088 KB
testcase_01 AC 82 ms
15,152 KB
testcase_02 AC 82 ms
15,172 KB
testcase_03 AC 94 ms
15,192 KB
testcase_04 AC 85 ms
15,092 KB
testcase_05 AC 104 ms
15,448 KB
testcase_06 AC 91 ms
15,344 KB
testcase_07 AC 89 ms
15,280 KB
testcase_08 AC 99 ms
15,460 KB
testcase_09 AC 111 ms
15,512 KB
testcase_10 AC 126 ms
15,812 KB
testcase_11 AC 109 ms
15,432 KB
testcase_12 AC 103 ms
15,456 KB
testcase_13 AC 89 ms
15,224 KB
testcase_14 AC 123 ms
15,776 KB
testcase_15 AC 132 ms
15,680 KB
testcase_16 AC 128 ms
15,664 KB
testcase_17 AC 131 ms
15,848 KB
testcase_18 AC 88 ms
15,084 KB
testcase_19 AC 133 ms
15,796 KB
testcase_20 AC 83 ms
15,096 KB
testcase_21 AC 83 ms
15,092 KB
testcase_22 AC 124 ms
15,544 KB
testcase_23 AC 132 ms
15,800 KB
testcase_24 AC 132 ms
15,756 KB
testcase_25 AC 131 ms
15,696 KB
testcase_26 AC 82 ms
15,040 KB
testcase_27 AC 92 ms
15,312 KB
testcase_28 AC 128 ms
15,688 KB
testcase_29 AC 109 ms
15,392 KB
testcase_30 AC 84 ms
15,252 KB
testcase_31 AC 84 ms
15,128 KB
testcase_32 AC 105 ms
15,396 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

n = gets.to_i

$nmoves = []
$bc = []

def bitcount(i)
  $bc[i] ||= i.to_s(2).count('1')
end

def move_from(i, n)
  return unless $nmoves[i]

  fwd = i + bitcount(i)
  if fwd <= n
    n_fwd = $nmoves[i] + 1
    if $nmoves[fwd].nil? || n_fwd < $nmoves[fwd]
      $nmoves[fwd] = n_fwd
      move_from(fwd, n)
    end
  end

  back = i - bitcount(i)
  return if back < 1

  n_back = $nmoves[i] + 1
  return if $nmoves[back] && $nmoves[back] <= n_back

  $nmoves[back] = n_back
  move_from(back, n)
end

$nmoves[1] = 1
1.upto(n-1) do |i|
  move_from(i, n)
end
puts $nmoves[n] || -1
0