結果

問題 No.6 使いものにならないハッシュ
ユーザー rarpipipi
提出日時 2024-04-20 11:41:27
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 84 ms / 5,000 ms
コード長 873 bytes
コンパイル時間 182 ms
コンパイル使用メモリ 82,108 KB
実行使用メモリ 78,612 KB
最終ジャッジ日時 2024-10-12 07:02:51
合計ジャッジ時間 3,710 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

# 素数判定
import math
def sieve_of_eratosthenes(n):
    prime = [True for i in range(n+1)]
    prime[0] = False
    prime[1] = False
    sqrt_n = math.ceil(math.sqrt(n))
    for i in range(2, sqrt_n):
        if prime[i]:
            for j in range(2*i, n+1, i):
                prime[j] = False

    return prime

def hash(x):
    while 10<=x:
        str_x=str(x)
        x=0
        for ele in str_x:
            x+=int(ele)
    return x
k=int(input())
n=int(input())
li=sieve_of_eratosthenes(n)
primes=[]
hashs=[]
for i in range(k,n+1):
    if li[i]:
        primes.append(i)
        hashs.append(hash(i))
l=len(primes)
ans=-1
ma=0

for i in range(l-1,-1,-1):
    dp=[0]*10
    dp[hashs[i]]=1
    j=0
    while i-j-1>=0 and dp[hashs[i-j-1]]==0:
        dp[hashs[i-j-1]]=1
        j+=1
    if ma<sum(dp):
        ma=sum(dp)
        ans=primes[i-j]
print(ans)
    
0