結果
| 問題 |
No.6 使いものにならないハッシュ
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2014-12-23 23:03:28 |
| 言語 | Ruby (3.4.1) |
| 結果 |
AC
|
| 実行時間 | 152 ms / 5,000 ms |
| コード長 | 2,217 bytes |
| コンパイル時間 | 221 ms |
| コンパイル使用メモリ | 7,552 KB |
| 実行使用メモリ | 13,184 KB |
| 最終ジャッジ日時 | 2024-09-16 16:23:18 |
| 合計ジャッジ時間 | 5,509 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 32 |
コンパイルメッセージ
Syntax OK
ソースコード
# 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