結果

問題 No.12 限定された素数
ユーザー nagitaosunagitaosu
提出日時 2020-03-13 11:35:54
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 457 ms / 5,000 ms
コード長 1,550 bytes
コンパイル時間 430 ms
コンパイル使用メモリ 87,396 KB
実行使用メモリ 150,736 KB
最終ジャッジ日時 2023-08-16 01:17:47
合計ジャッジ時間 12,643 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 411 ms
149,792 KB
testcase_01 AC 426 ms
150,220 KB
testcase_02 AC 421 ms
149,808 KB
testcase_03 AC 360 ms
149,892 KB
testcase_04 AC 406 ms
150,272 KB
testcase_05 AC 457 ms
149,808 KB
testcase_06 AC 410 ms
149,908 KB
testcase_07 AC 440 ms
149,852 KB
testcase_08 AC 432 ms
150,400 KB
testcase_09 AC 416 ms
150,736 KB
testcase_10 AC 441 ms
149,860 KB
testcase_11 AC 410 ms
149,720 KB
testcase_12 AC 427 ms
150,020 KB
testcase_13 AC 407 ms
149,872 KB
testcase_14 AC 437 ms
150,260 KB
testcase_15 AC 424 ms
149,812 KB
testcase_16 AC 430 ms
150,424 KB
testcase_17 AC 425 ms
149,504 KB
testcase_18 AC 409 ms
149,828 KB
testcase_19 AC 434 ms
149,912 KB
testcase_20 AC 413 ms
150,388 KB
testcase_21 AC 417 ms
149,948 KB
testcase_22 AC 405 ms
149,568 KB
testcase_23 AC 420 ms
149,872 KB
testcase_24 AC 408 ms
149,736 KB
testcase_25 AC 432 ms
149,704 KB
権限があれば一括ダウンロードができます

ソースコード

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