結果

問題 No.6 使いものにならないハッシュ
ユーザー siman
提出日時 2022-03-29 13:20:40
言語 Ruby
(3.4.1)
結果
AC  
実行時間 171 ms / 5,000 ms
コード長 936 bytes
コンパイル時間 127 ms
コンパイル使用メモリ 7,552 KB
実行使用メモリ 13,696 KB
最終ジャッジ日時 2024-11-08 02:11:41
合計ジャッジ時間 6,320 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 32
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

require 'prime'

K = gets.to_i
N = gets.to_i

def calc_hash(x)
  while x >= 10
    x = x.digits.sum
  end

  x
end

primes = Prime.each(N).to_a.select { |x| x >= K }
counter = Array.new(10, 0)
L = primes.size

left = 0
right = 0
counter[calc_hash(primes.first)] += 1
max_len = 1
ans = primes.first
conflicted = false

while left <= right
  if right == L - 1
    hash = calc_hash(primes[left])
    conflicted = false if counter[hash] == 2
    counter[hash] -= 1
    left += 1
  elsif conflicted
    hash = calc_hash(primes[left])
    conflicted = false if counter[hash] == 2
    counter[hash] -= 1
    left += 1
  else
    right += 1
    hash = calc_hash(primes[right])
    counter[hash] += 1

    conflicted = true if counter[hash] >= 2
  end

  if not conflicted
    len = right - left + 1

    if max_len < len
      max_len = len
      ans = primes[left]
    elsif max_len == len
      ans = primes[left]
    end
  end
end

puts ans
0