結果

問題 No.6 使いものにならないハッシュ
コンテスト
ユーザー roaris
提出日時 2019-10-27 20:24:26
言語 PyPy3
(8.0.0 + ACL)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 66 ms / 5,000 ms
+ 196µs
コード長 905 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 433 ms
コンパイル使用メモリ 65,512 KB
実行使用メモリ 62,204 KB
最終ジャッジ日時 2026-10-14 10:18:33
合計ジャッジ時間 4,034 ms
ジャッジサーバーID
(参考情報)
judge5_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

from collections import defaultdict

def era(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 not is_prime[i]:
            continue
        
        for j in range(2*i, n+1, i):
            is_prime[j] = False
    
    return [i for i in range(K, n+1) if is_prime[i]]
    
def calc(n):
    if len(str(n)) == 1:
        return n
    
    s = 0
    
    for ni in str(n):
        s += int(ni)
    
    return calc(s)

K = int(input())
N = int(input())
primes = era(N)
hashed = [calc(p) for p in primes]

r = 0
length = 0
d = defaultdict(int)

for l in range(len(hashed)):
    while r < len(hashed) and d[hashed[r]] == 0:
        d[hashed[r]] += 1
        r += 1
    
    if length <= r-l:
        length = r-l
        ans = primes[l]
        
    if l == r:
        r += 1
    else:
        d[hashed[l]] -= 1

print(ans)
0