結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 21 ms
8,912 KB
testcase_01 AC 21 ms
9,068 KB
testcase_02 AC 127 ms
12,864 KB
testcase_03 AC 37 ms
9,484 KB
testcase_04 AC 47 ms
10,072 KB
testcase_05 AC 51 ms
10,008 KB
testcase_06 AC 80 ms
10,924 KB
testcase_07 AC 61 ms
10,452 KB
testcase_08 AC 74 ms
10,940 KB
testcase_09 AC 64 ms
10,852 KB
testcase_10 RE -
testcase_11 AC 40 ms
9,568 KB
testcase_12 AC 92 ms
11,452 KB
testcase_13 AC 57 ms
10,500 KB
testcase_14 AC 62 ms
10,656 KB
testcase_15 AC 77 ms
10,980 KB
testcase_16 AC 71 ms
11,088 KB
testcase_17 AC 101 ms
11,708 KB
testcase_18 AC 121 ms
12,688 KB
testcase_19 AC 98 ms
11,780 KB
testcase_20 AC 88 ms
11,480 KB
testcase_21 AC 32 ms
9,280 KB
testcase_22 AC 95 ms
11,428 KB
testcase_23 AC 88 ms
11,212 KB
testcase_24 AC 90 ms
11,572 KB
testcase_25 AC 65 ms
10,676 KB
testcase_26 AC 105 ms
12,084 KB
testcase_27 AC 87 ms
11,404 KB
testcase_28 AC 62 ms
10,580 KB
testcase_29 AC 110 ms
12,004 KB
testcase_30 AC 101 ms
11,812 KB
testcase_31 AC 92 ms
11,548 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 + 1, 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