結果

問題 No.3 ビットすごろく
ユーザー simansiman
提出日時 2023-06-26 16:54:26
言語 Ruby
(3.3.0)
結果
AC  
実行時間 94 ms / 5,000 ms
コード長 851 bytes
コンパイル時間 114 ms
コンパイル使用メモリ 11,232 KB
実行使用メモリ 15,784 KB
最終ジャッジ日時 2023-09-16 10:58:15
合計ジャッジ時間 4,510 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 80 ms
15,328 KB
testcase_01 AC 81 ms
15,240 KB
testcase_02 AC 80 ms
15,272 KB
testcase_03 AC 84 ms
15,204 KB
testcase_04 AC 81 ms
15,220 KB
testcase_05 AC 86 ms
15,372 KB
testcase_06 AC 84 ms
15,404 KB
testcase_07 AC 83 ms
15,268 KB
testcase_08 AC 88 ms
15,420 KB
testcase_09 AC 89 ms
15,412 KB
testcase_10 AC 92 ms
15,652 KB
testcase_11 AC 90 ms
15,304 KB
testcase_12 AC 89 ms
15,568 KB
testcase_13 AC 85 ms
15,312 KB
testcase_14 AC 92 ms
15,784 KB
testcase_15 AC 93 ms
15,720 KB
testcase_16 AC 94 ms
15,704 KB
testcase_17 AC 94 ms
15,648 KB
testcase_18 AC 85 ms
15,104 KB
testcase_19 AC 94 ms
15,592 KB
testcase_20 AC 83 ms
15,132 KB
testcase_21 AC 82 ms
15,244 KB
testcase_22 AC 90 ms
15,680 KB
testcase_23 AC 92 ms
15,556 KB
testcase_24 AC 92 ms
15,656 KB
testcase_25 AC 94 ms
15,672 KB
testcase_26 AC 81 ms
15,180 KB
testcase_27 AC 85 ms
15,240 KB
testcase_28 AC 91 ms
15,672 KB
testcase_29 AC 90 ms
15,436 KB
testcase_30 AC 80 ms
15,240 KB
testcase_31 AC 79 ms
15,164 KB
testcase_32 AC 86 ms
15,344 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

class Integer
  def bitcount
    n = self
    count = 0

    while n > 0
      count += n & 1
      n >>= 1
    end

    count
  end
end

class Yukicoder
  UNKNOWN = -1

  def initialize
    n = gets.chomp.to_i

    check_list = Hash.new(UNKNOWN)
    check_list[1] = 1
    dist = 2
    states = [1]

    while states.any?
      next_states = []

      states.each do |s|
        move_dist = s.bitcount
        a = s - move_dist
        b = s + move_dist

        if a >= 1 && check_list[a] == UNKNOWN
          check_list[s - move_dist] = dist
          next_states << s - move_dist
        end

        if b <= n && check_list[b] == UNKNOWN
          check_list[s + move_dist] = dist
          next_states << s + move_dist
        end
      end

      states = next_states.dup
      dist += 1
    end

    puts check_list[n]
  end
end

Yukicoder.new
0