結果

問題 No.12 限定された素数
ユーザー pessimist
提出日時 2025-06-08 21:56:18
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 341 ms / 5,000 ms
コード長 812 bytes
コンパイル時間 447 ms
コンパイル使用メモリ 82,156 KB
実行使用メモリ 138,304 KB
最終ジャッジ日時 2025-06-08 21:56:29
合計ジャッジ時間 8,908 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

LIM=5*10**6+1
isp=[False]*LIM
isp[2]=True
for p in range(3,LIM,2):isp[p]=True
for p in range(3,LIM,2):
    if p*p>=LIM:continue
    if not isp[p]: continue
    for j in range(p*p,LIM,p+p):isp[j]=False

N,*A=map(int,read().split())
full=0
for x in A: full |= 1<<x

def num_to_set(n):
    ret=0
    while n:
        n,r=divmod(n,10)
        ret|=1<<r
    return ret

ng=[]
ok=[]

for p in range(LIM):
    if not isp[p]: continue
    se=num_to_set(p)
    if se & (~full):ng.append(p)
    else: ok.append((p, se))

ng.append(LIM)
ok.append((LIM,0))

L=1
i=0
ans=-1
for x in ng:
    R=x-1
    se=0
    while True:
        p, s = ok[i]
        if p>R:break
        se|=s
        i+=1
    if se==full and ans<R-L:ans=R-L
    L=x+1
print(ans)
0