結果

問題 No.6 使いものにならないハッシュ
ユーザー rpy3cpprpy3cpp
提出日時 2015-08-06 23:44:27
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 49 ms / 5,000 ms
コード長 1,421 bytes
コンパイル時間 136 ms
コンパイル使用メモリ 10,996 KB
実行使用メモリ 10,524 KB
最終ジャッジ日時 2023-10-14 22:53:56
合計ジャッジ時間 2,244 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
8,528 KB
testcase_01 AC 14 ms
8,532 KB
testcase_02 AC 49 ms
10,524 KB
testcase_03 AC 21 ms
8,700 KB
testcase_04 AC 21 ms
9,060 KB
testcase_05 AC 25 ms
9,116 KB
testcase_06 AC 34 ms
9,596 KB
testcase_07 AC 27 ms
9,560 KB
testcase_08 AC 31 ms
9,568 KB
testcase_09 AC 25 ms
10,168 KB
testcase_10 AC 15 ms
8,328 KB
testcase_11 AC 21 ms
8,644 KB
testcase_12 AC 38 ms
9,600 KB
testcase_13 AC 23 ms
9,636 KB
testcase_14 AC 25 ms
10,000 KB
testcase_15 AC 31 ms
9,456 KB
testcase_16 AC 28 ms
9,664 KB
testcase_17 AC 38 ms
9,988 KB
testcase_18 AC 47 ms
10,400 KB
testcase_19 AC 38 ms
10,008 KB
testcase_20 AC 35 ms
9,736 KB
testcase_21 AC 18 ms
8,724 KB
testcase_22 AC 35 ms
9,728 KB
testcase_23 AC 36 ms
9,600 KB
testcase_24 AC 35 ms
9,640 KB
testcase_25 AC 28 ms
9,528 KB
testcase_26 AC 39 ms
10,200 KB
testcase_27 AC 35 ms
9,692 KB
testcase_28 AC 28 ms
9,288 KB
testcase_29 AC 43 ms
10,092 KB
testcase_30 AC 40 ms
10,008 KB
testcase_31 AC 36 ms
9,772 KB
権限があれば一括ダウンロードができます

ソースコード

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