結果

問題 No.6 使いものにならないハッシュ
ユーザー rpy3cpp
提出日時 2015-08-06 23:44:27
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 59 ms / 5,000 ms
コード長 1,421 bytes
コンパイル時間 86 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 12,748 KB
最終ジャッジ日時 2024-09-16 16:28:24
合計ジャッジ時間 2,345 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

def primes2(limit):
    ''' returns a list of prime numbers upto limit.
    source: Rossetta code: Sieve of Eratosthenes
    http://rosettacode.org/wiki/Sieve_of_Eratosthenes#Odds-only_version_of_the_array_sieve_above
    '''
    if limit < 2: return []
    if limit < 3: return [2]
    lmtbf = (limit - 3) // 2
    buf = [True] * (lmtbf + 1)
    for i in range((int(limit ** 0.5) - 3) // 2 + 1):
        if buf[i]:
            p = i + i + 3
            s = p * (i + 1) + i
            buf[s::p] = [False] * ((lmtbf - s) // p + 1)
    return [2] + [i + i + 3 for i, v in enumerate(buf) if v]


def h(p):
    p, r = divmod(p, 10)
    cump = r
    while p >= 10:
        p, r = divmod(p, 10)
        cump += r
    cump += p
    if cump < 10:
        return cump
    else:
        return h(cump)


def solve(K, N):
    hashed = [(h(p), p) for p in primes2(N) if p >= K]
    head = 0
    tail = 0
    freq = [0] * 10
    freq[hashed[0][0]] = 1
    record = 1
    mark = hashed[0][1]
    goal = len(hashed) - 1
    score = 1
    while tail < goal:
        tail += 1
        c, p = hashed[tail]
        freq[c] += 1
        while freq[c] >= 2:
            cc, pp = hashed[head]
            freq[cc] -= 1
            score -= 1
            head += 1
        score += 1
        if score >= record:
            record = score
            mark = hashed[head][1]
    return mark

K = int(input())
N = int(input())
print(solve(K, N))
0