結果

問題 No.12 限定された素数
ユーザー nsd_fbnsd_fb
提出日時 2015-02-19 07:17:54
言語 Python2
(2.7.18)
結果
AC  
実行時間 3,020 ms / 5,000 ms
コード長 1,079 bytes
コンパイル時間 207 ms
コンパイル使用メモリ 6,704 KB
実行使用メモリ 56,084 KB
最終ジャッジ日時 2023-08-15 22:47:33
合計ジャッジ時間 80,016 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2,948 ms
56,076 KB
testcase_01 AC 2,909 ms
56,084 KB
testcase_02 AC 2,863 ms
56,072 KB
testcase_03 AC 2,723 ms
55,888 KB
testcase_04 AC 2,892 ms
55,888 KB
testcase_05 AC 2,903 ms
55,900 KB
testcase_06 AC 2,865 ms
55,896 KB
testcase_07 AC 2,908 ms
56,008 KB
testcase_08 AC 2,880 ms
55,888 KB
testcase_09 AC 2,931 ms
56,036 KB
testcase_10 AC 2,909 ms
55,968 KB
testcase_11 AC 2,809 ms
55,988 KB
testcase_12 AC 2,831 ms
56,032 KB
testcase_13 AC 2,910 ms
56,020 KB
testcase_14 AC 2,869 ms
56,080 KB
testcase_15 AC 2,887 ms
55,892 KB
testcase_16 AC 2,866 ms
55,884 KB
testcase_17 AC 3,020 ms
55,964 KB
testcase_18 AC 2,895 ms
56,068 KB
testcase_19 AC 3,007 ms
55,960 KB
testcase_20 AC 2,925 ms
55,968 KB
testcase_21 AC 2,915 ms
56,064 KB
testcase_22 AC 2,904 ms
55,892 KB
testcase_23 AC 2,892 ms
55,884 KB
testcase_24 AC 2,978 ms
55,968 KB
testcase_25 AC 2,911 ms
56,020 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