結果

問題 No.6 使いものにならないハッシュ
ユーザー らっしー(raccy)らっしー(raccy)
提出日時 2014-12-23 23:03:28
言語 Ruby
(3.2.2)
結果
AC  
実行時間 149 ms / 5,000 ms
コード長 2,217 bytes
コンパイル時間 364 ms
コンパイル使用メモリ 11,436 KB
実行使用メモリ 16,536 KB
最終ジャッジ日時 2023-10-14 22:48:09
合計ジャッジ時間 5,630 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 93 ms
15,344 KB
testcase_01 AC 94 ms
15,204 KB
testcase_02 AC 149 ms
16,108 KB
testcase_03 AC 109 ms
15,424 KB
testcase_04 AC 120 ms
15,704 KB
testcase_05 AC 120 ms
15,648 KB
testcase_06 AC 140 ms
16,208 KB
testcase_07 AC 134 ms
16,080 KB
testcase_08 AC 137 ms
16,092 KB
testcase_09 AC 132 ms
15,892 KB
testcase_10 AC 96 ms
15,244 KB
testcase_11 AC 109 ms
15,644 KB
testcase_12 AC 142 ms
16,276 KB
testcase_13 AC 131 ms
16,092 KB
testcase_14 AC 133 ms
15,892 KB
testcase_15 AC 139 ms
16,416 KB
testcase_16 AC 135 ms
16,320 KB
testcase_17 AC 142 ms
16,036 KB
testcase_18 AC 149 ms
16,276 KB
testcase_19 AC 141 ms
15,944 KB
testcase_20 AC 140 ms
15,912 KB
testcase_21 AC 102 ms
15,504 KB
testcase_22 AC 142 ms
15,956 KB
testcase_23 AC 140 ms
16,284 KB
testcase_24 AC 141 ms
16,016 KB
testcase_25 AC 133 ms
16,036 KB
testcase_26 AC 146 ms
16,220 KB
testcase_27 AC 140 ms
15,888 KB
testcase_28 AC 120 ms
15,648 KB
testcase_29 AC 144 ms
16,536 KB
testcase_30 AC 144 ms
16,248 KB
testcase_31 AC 139 ms
16,020 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

# coding: utf-8
require "prime"

# プッチ神父クラス
class Pucci
  # 落ちつけ…………
  def initialize(begin_num, end_num)
    @begin_num = begin_num
    @end_num = end_num
    # 今持っている素数のリスト
    @prime_list = []
    # そのハッシュのリスト
    @prime_hash_list = []
    # 今までで最大のリストの長さ
    @max_length = 0
    # そのときの素数
    @prime_of_max_length = nil
  end

  # 心を平静にして考えるんだ…
  # こんな時どうするか……
  def calc_useless_hash(num)
    sum = 0
    while num > 0
      sum += num % 10
      num /= 10
    end
    if sum < 10
      return sum
    else
      return calc_useless_hash(sum)
    end
  end

  # 落ちつくんだ…
  # 「素数」を数えて落ちつくんだ…
  def prime_each
    Prime.instance.each do |prime|
      if prime < @begin_num
        # 2…3…5…
        next
      elsif prime <= @end_num
        # 7…11…13…17…19…
        yield prime
      else
        # 2**57885161−1 「時の加速」により「加速」の行きつく究極の所!
        break
      end
    end
  end

  # 「素数」は1と自分の数でしか割ることのできない孤独な数字……
  # わたしに勇気を与えてくれる
  def give_prime(prime)
    prime_hash = calc_useless_hash(prime)
    if @prime_hash_list.include?(prime_hash)
      collision_index = @prime_hash_list.index(prime_hash)
      # 神の御命においてしりぞけるッ!
      @prime_list.shift(collision_index + 1)
      @prime_hash_list.shift(collision_index + 1)
    end
    @prime_list.push(prime)
    @prime_hash_list.push(prime_hash)
    if @prime_list.size >= @max_length
      # 「宇宙」は一巡したッ!「新しい世界」だッ!
      @prime_of_max_length = @prime_list[0]
      @max_length = @prime_list.size
    end
  end

  # 人類は一つの終点に到着し『夜明け』を迎えたのだッ!
  def say_answer_of_life_universe_everything
    puts @prime_of_max_length || 42
  end
end

k = gets.to_i
n = gets.to_i

pucci = Pucci.new(k, n)
pucci.prime_each do |prime|
  pucci.give_prime(prime)
end
pucci.say_answer_of_life_universe_everything
0