結果

問題 No.6 使いものにならないハッシュ
ユーザー しらっ亭
提出日時 2015-08-30 18:36:50
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 74 ms / 5,000 ms
コード長 976 bytes
コンパイル時間 77 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 13,056 KB
最終ジャッジ日時 2024-09-16 16:29:15
合計ジャッジ時間 2,861 ms
ジャッジサーバーID
(参考情報)
judge6 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

def mark(s, x):
    for i in range(x + x, len(s), x):
        s[i] = False


def sieve(n):
    s = [True] * n
    for x in range(2, int(n ** 0.5) + 1):
        if s[x]:
            mark(s, x)
    return [i for i in range(n) if s[i] and i > 1]


def dr(n):
    if n == 0:
        return 0
    if n % 9 == 0:
        return 9
    else:
        return n % 9


def solve():
    K = int(input())
    N = int(input())

    hc = [0] * 10
    tc = 0

    ps = [p for p in sieve(N + 1) if p >= K]

    i = j = 0
    ma = 0
    mi = 0

    while i < len(ps):
        if tc == 0 and j < len(ps):
            h = dr(ps[j])
            hc[h] += 1
            if hc[h] >= 2:
                tc += 1
            j += 1
        else:
            h = dr(ps[i])
            hc[h] -= 1
            if hc[h] == 1:
                tc -= 1
            i += 1

        if tc == 0 and j - i >= ma:
            ma = j - i
            mi = i

    print(ps[mi])


if __name__ == '__main__':
    solve()
0