結果

問題 No.12 限定された素数
ユーザー rpy3cpprpy3cpp
提出日時 2015-08-07 02:18:52
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 775 ms / 5,000 ms
コード長 1,583 bytes
コンパイル時間 1,979 ms
コンパイル使用メモリ 10,964 KB
実行使用メモリ 46,776 KB
最終ジャッジ日時 2023-08-15 23:06:16
合計ジャッジ時間 11,816 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 290 ms
46,628 KB
testcase_01 AC 333 ms
46,752 KB
testcase_02 AC 298 ms
46,668 KB
testcase_03 AC 775 ms
46,704 KB
testcase_04 AC 315 ms
46,732 KB
testcase_05 AC 379 ms
46,760 KB
testcase_06 AC 483 ms
46,708 KB
testcase_07 AC 528 ms
46,620 KB
testcase_08 AC 338 ms
46,776 KB
testcase_09 AC 343 ms
46,696 KB
testcase_10 AC 332 ms
46,756 KB
testcase_11 AC 635 ms
46,588 KB
testcase_12 AC 517 ms
46,624 KB
testcase_13 AC 437 ms
46,636 KB
testcase_14 AC 344 ms
46,764 KB
testcase_15 AC 441 ms
46,704 KB
testcase_16 AC 560 ms
46,588 KB
testcase_17 AC 304 ms
46,696 KB
testcase_18 AC 294 ms
46,584 KB
testcase_19 AC 289 ms
46,648 KB
testcase_20 AC 298 ms
46,756 KB
testcase_21 AC 330 ms
46,588 KB
testcase_22 AC 300 ms
46,692 KB
testcase_23 AC 300 ms
46,764 KB
testcase_24 AC 299 ms
46,660 KB
testcase_25 AC 360 ms
46,704 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 read_data():
    N = int(input())
    A = list(map(int, input().split()))
    return N, A


def solve(N, A):
    limit = 5 * 10**6
    primes = primes2(limit)
    NGs = [True] * 10
    for a in A:
        NGs[a] = False
    OKset = set(A)
    prev_p = 0
    maxspan = -1
    currentset = set()
    for p in primes:
        if is_safe(p, NGs):
            update_set(currentset, p)
            continue
        elif currentset == OKset:
            span = p - prev_p - 2
            if maxspan < span:
                maxspan = span
        currentset = set()
        prev_p = p
    p = limit
    if currentset == OKset:
        span = p - prev_p - 1
        if maxspan < span:
            maxspan = span
    return maxspan

def is_safe(p, NGs):
    while p:
        p, r = divmod(p, 10)
        if NGs[r]:
            return False
    return True

def update_set(currentset, p):
    while p:
        p, r = divmod(p, 10)
        currentset.add(r)

N, A = read_data()
print(solve(N, A))
0