結果

問題 No.12 限定された素数
ユーザー nsd_fbnsd_fb
提出日時 2015-02-19 07:19:59
言語 PyPy2
(7.3.15)
結果
AC  
実行時間 642 ms / 5,000 ms
コード長 1,079 bytes
コンパイル時間 2,083 ms
コンパイル使用メモリ 77,400 KB
実行使用メモリ 137,048 KB
最終ジャッジ日時 2023-08-15 22:44:21
合計ジャッジ時間 18,908 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 562 ms
136,908 KB
testcase_01 AC 599 ms
136,680 KB
testcase_02 AC 543 ms
136,676 KB
testcase_03 AC 498 ms
136,696 KB
testcase_04 AC 577 ms
136,744 KB
testcase_05 AC 618 ms
136,668 KB
testcase_06 AC 568 ms
136,848 KB
testcase_07 AC 574 ms
136,588 KB
testcase_08 AC 585 ms
136,568 KB
testcase_09 AC 587 ms
137,048 KB
testcase_10 AC 639 ms
136,784 KB
testcase_11 AC 578 ms
136,716 KB
testcase_12 AC 596 ms
136,764 KB
testcase_13 AC 596 ms
136,588 KB
testcase_14 AC 611 ms
136,972 KB
testcase_15 AC 582 ms
136,960 KB
testcase_16 AC 582 ms
136,752 KB
testcase_17 AC 564 ms
136,656 KB
testcase_18 AC 573 ms
136,708 KB
testcase_19 AC 580 ms
136,784 KB
testcase_20 AC 564 ms
136,584 KB
testcase_21 AC 595 ms
136,784 KB
testcase_22 AC 579 ms
136,652 KB
testcase_23 AC 580 ms
136,572 KB
testcase_24 AC 571 ms
136,660 KB
testcase_25 AC 642 ms
137,040 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def sieve(n):
    res = []
    is_prime = [True] * (n + 1)
    
    for i in xrange(2, n + 1):
        if is_prime[i]:
            res.append(i)

            for j in xrange(i * i, n + 1, i):
                is_prime[j] = False

    return res

def ng(need, numbers):
    for d in numbers:
        if not need[d]:
            return True
        
    return False

def ok(need, exist):
    for n, e in zip(need, exist):
        if n and not e:
            return False

    return True

primes = sieve(5000000)
n = input()
need = [False] * 10

for a in map(int, raw_input().split()):
    need[a] = True

ans = -1
prev = 1

l = 0
while l < len(primes):
    r = l
    exist = [False] * 10

    while r < len(primes):
        numbers = map(int, str(primes[r]))

        if ng(need, numbers):
            break

        for d in numbers:
            exist[d] = True

        r += 1

    current = primes[r] - 1 if r < len(primes) else 5000000
    if ok(need, exist):
        diff = current - prev
        ans = max(ans, diff)
        
    prev = current + 2
    l = r + 1

print ans
0