結果

問題 No.6 使いものにならないハッシュ
ユーザー shimon
提出日時 2024-06-10 17:13:28
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 95 ms / 5,000 ms
コード長 647 bytes
コンパイル時間 302 ms
コンパイル使用メモリ 82,128 KB
実行使用メモリ 78,072 KB
最終ジャッジ日時 2025-01-02 11:00:50
合計ジャッジ時間 3,620 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

k=int(input())
n=int(input())

def get_primes(n):
  is_prime = [True] * (n + 1)
  is_prime[0] = False
  is_prime[1] = False
  for i in range(2, int(n ** 0.5) + 1):
    if is_prime[i]:
      for j in range(i * i, n + 1, i):
        is_prime[j] = False
  return [i for i in range(n + 1) if is_prime[i] and i>=k]

def to_hash(x):
  while x>9:
    x=sum(map(int, str(x)))
  return x

primes = get_primes(n)
hashes = [to_hash(prime) for prime in primes]

ans=0
idx=-1

for i in range(len(primes))[::-1]:
  s=set()
  j=0
  while i+j<len(primes) and hashes[i+j] not in s:
    s.add(hashes[i+j])
    j+=1
  if ans<j:
    ans=j
    idx=i
print(primes[idx])
0