結果

問題 No.6 使いものにならないハッシュ
ユーザー nisizawanisizawa
提出日時 2017-12-19 15:57:00
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
RE  
(最新)
AC  
(最初)
実行時間 -
コード長 906 bytes
コンパイル時間 162 ms
コンパイル使用メモリ 10,832 KB
実行使用メモリ 12,916 KB
最終ジャッジ日時 2023-10-14 23:10:15
合計ジャッジ時間 3,813 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 21 ms
8,820 KB
testcase_01 AC 21 ms
8,932 KB
testcase_02 AC 129 ms
12,916 KB
testcase_03 AC 38 ms
9,436 KB
testcase_04 AC 48 ms
9,912 KB
testcase_05 AC 52 ms
10,052 KB
testcase_06 AC 81 ms
11,072 KB
testcase_07 AC 61 ms
10,304 KB
testcase_08 AC 75 ms
10,872 KB
testcase_09 AC 65 ms
10,592 KB
testcase_10 RE -
testcase_11 AC 41 ms
9,488 KB
testcase_12 AC 92 ms
11,296 KB
testcase_13 AC 58 ms
10,108 KB
testcase_14 AC 63 ms
10,584 KB
testcase_15 AC 78 ms
10,764 KB
testcase_16 AC 72 ms
10,764 KB
testcase_17 AC 103 ms
11,892 KB
testcase_18 AC 121 ms
12,460 KB
testcase_19 AC 101 ms
11,616 KB
testcase_20 AC 89 ms
11,412 KB
testcase_21 AC 31 ms
9,176 KB
testcase_22 AC 94 ms
11,576 KB
testcase_23 AC 90 ms
11,508 KB
testcase_24 AC 91 ms
11,348 KB
testcase_25 AC 66 ms
10,620 KB
testcase_26 AC 107 ms
12,020 KB
testcase_27 AC 88 ms
11,352 KB
testcase_28 AC 64 ms
10,492 KB
testcase_29 AC 111 ms
12,268 KB
testcase_30 AC 100 ms
11,632 KB
testcase_31 AC 93 ms
11,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from functools import lru_cache

def prime_ls(k, n):
    ls = [False if i % 2 == 0 or i % 3 == 0 or i % 5 == 0 else True for i in range(n + 1)]
    ls[0] = ls[1] = False
    ls[2] = ls[3] = ls[5] = True

    for i in range(3, int(n**0.5) + 1, 2):
        for j in range(i**2, n, i): 
            ls[j] = False
    
    return [p for p, is_prime in enumerate(ls[k:], k) if is_prime]

@lru_cache(maxsize=None)
def hash(n):
    s = str(n)
    return hash(sum([int(c) for c in s])) if len(s) >= 2 else n

k = int(input())
n = int(input())

p_ls = prime_ls(k, n)
h_ls = [hash(p) for p in p_ls]

l, r = 0, 0
max_len = 0

memo = [False] * 10

while r < len(h_ls):
    hl, hr = h_ls[l], h_ls[r]
    
    if memo[hr]:
        memo[hl] = False
        l += 1
    else:
        memo[hr] = True
        r += 1
    
    lr_len = r - l
    if lr_len >= max_len:
        max_len = lr_len
        ans = p_ls[l]

print(ans)
0