結果

問題 No.12 限定された素数
ユーザー nagitaosu
提出日時 2020-03-13 11:35:54
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 400 ms / 5,000 ms
コード長 1,550 bytes
コンパイル時間 130 ms
コンパイル使用メモリ 82,464 KB
実行使用メモリ 149,320 KB
最終ジャッジ日時 2024-11-24 10:26:23
合計ジャッジ時間 10,826 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3
import sys
input = sys.stdin.readline

def primes(n):
    is_prime = [True] * (n + 1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5) + 1):
        if not is_prime[i]:
            continue
        for j in range(i * 2, n + 1, i):
            is_prime[j] = False
    return [i for i in range(n + 1) if is_prime[i]]

n = int(input())
ref = [False] * 10 
a = [int(item) for item in input().split()]
for item in a:
    ref[item] = True
max_n = 5 * 10**6
p = [0] + primes(max_n) + [5*10**6 + 1]
cnt = [0] * 10
max_len = 0
ans = None
l = 1
for r in range(1, len(p) - 1):
    # Append
    s = str(p[r])
    for ch in s:
        cnt[int(ch)] += 1
    # Detect over / short / same
    state = "same"
    for j, item in enumerate(cnt):
        if not ref[j] and item > 0:
            state = "over"
            break
        elif ref[j] and not item > 0:
            state = "short"
            break
    while state == "over":
        s = str(p[l])
        l += 1
        for ch in s:
            cnt[int(ch)] -= 1
        state = "same"
        for j, item in enumerate(cnt):
            if not ref[j] and item > 0:
                state = "over"
                break
            elif ref[j] and not item > 0:
                state = "short"
                break
    if state == "same":
        if p[r+1] - p[l-1] > max_len:
            max_len = p[r+1] - p[l-1]
            ans = p[r + 1] - p[l - 1] - 2
    elif state == "short":
        continue
if ans == None:
    print(-1)
else:
    print(ans)
0