結果

問題 No.12 限定された素数
ユーザー Mao-betaMao-beta
提出日時 2024-02-29 00:35:28
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 400 ms / 5,000 ms
コード長 2,882 bytes
コンパイル時間 261 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 140,808 KB
最終ジャッジ日時 2024-02-29 00:35:39
合計ジャッジ時間 10,409 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 319 ms
140,680 KB
testcase_01 AC 328 ms
140,676 KB
testcase_02 AC 315 ms
140,568 KB
testcase_03 AC 204 ms
130,848 KB
testcase_04 AC 320 ms
140,568 KB
testcase_05 AC 330 ms
140,804 KB
testcase_06 AC 341 ms
140,800 KB
testcase_07 AC 367 ms
140,808 KB
testcase_08 AC 331 ms
140,672 KB
testcase_09 AC 325 ms
140,676 KB
testcase_10 AC 320 ms
140,680 KB
testcase_11 AC 400 ms
140,800 KB
testcase_12 AC 364 ms
140,808 KB
testcase_13 AC 326 ms
140,808 KB
testcase_14 AC 343 ms
140,804 KB
testcase_15 AC 331 ms
140,808 KB
testcase_16 AC 384 ms
140,808 KB
testcase_17 AC 293 ms
140,536 KB
testcase_18 AC 316 ms
140,540 KB
testcase_19 AC 309 ms
140,540 KB
testcase_20 AC 299 ms
140,536 KB
testcase_21 AC 318 ms
140,664 KB
testcase_22 AC 320 ms
140,568 KB
testcase_23 AC 304 ms
140,568 KB
testcase_24 AC 291 ms
140,536 KB
testcase_25 AC 330 ms
140,804 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import math
import bisect
from heapq import heapify, heappop, heappush
from collections import deque, defaultdict, Counter
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
MOD99 = 998244353

input = lambda: sys.stdin.readline().strip()
NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()
SMI = lambda: input().split()
SLI = lambda: list(SMI())
EI = lambda m: [NLI() for _ in range(m)]


# 高速エラストテネス sieve[n]はnの最小の素因数
def make_prime_table(n):
    sieve = list(range(n + 1))
    sieve[0] = -1
    sieve[1] = -1
    for i in range(4, n + 1, 2):
        sieve[i] = 2
    for i in range(3, int(n ** 0.5) + 1, 2):
        if sieve[i] != i:
            continue
        for j in range(i * i, n + 1, i * 2):
            if sieve[j] == j:
                sieve[j] = i
    return sieve

prime_table = make_prime_table(5000001)
# 素数列挙
primes = [p for i, p in enumerate(prime_table) if i == p]

# 素因数分解 上のprime_tableと組み合わせて使う
def prime_factorize(n):
    result = []
    while n != 1:
        p = prime_table[n]
        e = 0
        while n % p == 0:
            n //= p
            e += 1
        result.append((p, e))
    return result


# Nの素因数分解を辞書で返す(単体)
def prime_fact(n):
    root = int(n**0.5) + 1
    prime_dict = {}
    for i in range(2, root):
        cnt = 0
        while n % i == 0:
            cnt += 1
            n = n // i
        if cnt:
            prime_dict[i] = cnt
    if n != 1:
        prime_dict[n] = 1
    return prime_dict

# 約数列挙(単体)
def divisors(x):
    res = set()
    for i in range(1, int(x**0.5) + 2):
        if x % i == 0:
            res.add(i)
            res.add(x//i)
    return res


def main():
    N = NI()
    A = set(SLI())

    if len(A) == 10:
        print(4999999)
        return

    PN = len(primes)
    ans = -1
    l = 0
    r = 0
    # print(A)
    while l < PN and r < PN:
        S = set()
        r = l
        K = 0
        L = 0
        for i in range(l, PN):
            p = str(primes[i])
            ok = True
            for sp in p:
                if sp not in A:
                    ok = False
            if ok:
                S |= set(list(p))
            else:
                r = i
                break

        if S == A and l < r:
            if l == 0:
                K = 1
            else:
                K = primes[l-1] + 1

            if r == PN:
                L = 5000000
            else:
                L = primes[r] - 1

            ans = max(ans, L-K)
            l = r

        elif l < r:
            l = r

        elif l == r:
            l += 1

    print(ans)


if __name__ == "__main__":
    main()
0