結果

問題 No.6 使いものにならないハッシュ
ユーザー rlangevin
提出日時 2023-10-02 08:58:07
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 83 ms / 5,000 ms
コード長 820 bytes
コンパイル時間 274 ms
コンパイル使用メモリ 82,372 KB
実行使用メモリ 80,544 KB
最終ジャッジ日時 2024-07-26 13:36:49
合計ジャッジ時間 3,771 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

from math import sqrt, ceil


def Sieve(n):
    lst = [True] * (n + 1)
    S = set()
    for i in range(2, ceil(sqrt(n)) + 1):
        if lst[i]:
            for j in range(2 * i, n + 1, i):
                lst[j] = False
    for i in range(2, n + 1):
        if lst[i]:
            S.add(i)
    return sorted(list(S))

K = int(input())
N = int(input())
S = Sieve(N + 5)

from collections import *
Q = deque()
seen = [0] * 10

def f(x):
    while len(str(x)) != 1:
        v = 0
        while x:
            v += x % 10
            x //= 10
        x = v
    return x

ans = -1
maxv = -1
for s in S:
    if s < K or s > N:
        continue
    q = f(s)
    seen[q] += 1
    Q.append(s)
    while seen[q] == 2:
        seen[f(Q.popleft())] -= 1

    if len(Q) >= maxv:
        ans = Q[0]
        maxv = len(Q)

print(ans)
0