結果

問題 No.12 限定された素数
ユーザー convexineqconvexineq
提出日時 2021-01-05 09:54:28
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,730 ms / 5,000 ms
コード長 1,439 bytes
コンパイル時間 218 ms
コンパイル使用メモリ 82,816 KB
実行使用メモリ 142,452 KB
最終ジャッジ日時 2024-04-23 16:15:37
合計ジャッジ時間 39,789 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,370 ms
142,208 KB
testcase_01 AC 1,561 ms
142,208 KB
testcase_02 AC 1,406 ms
142,216 KB
testcase_03 AC 1,104 ms
141,952 KB
testcase_04 AC 1,470 ms
142,208 KB
testcase_05 AC 1,344 ms
142,080 KB
testcase_06 AC 1,583 ms
142,204 KB
testcase_07 AC 1,598 ms
142,208 KB
testcase_08 AC 1,339 ms
142,080 KB
testcase_09 AC 1,380 ms
142,208 KB
testcase_10 AC 1,442 ms
142,024 KB
testcase_11 AC 1,730 ms
142,124 KB
testcase_12 AC 1,698 ms
142,208 KB
testcase_13 AC 1,592 ms
142,152 KB
testcase_14 AC 1,431 ms
142,168 KB
testcase_15 AC 1,630 ms
142,208 KB
testcase_16 AC 1,353 ms
142,404 KB
testcase_17 AC 1,480 ms
142,344 KB
testcase_18 AC 1,309 ms
142,080 KB
testcase_19 AC 1,307 ms
142,452 KB
testcase_20 AC 1,283 ms
142,300 KB
testcase_21 AC 1,444 ms
142,276 KB
testcase_22 AC 1,341 ms
142,256 KB
testcase_23 AC 1,376 ms
142,336 KB
testcase_24 AC 1,396 ms
142,208 KB
testcase_25 AC 1,429 ms
142,276 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def Eratosthenes(N): #N以下の素数のリストを返す
    N+=1
    is_prime_list = [True]*N
    is_prime_list[0], is_prime_list[1] = False, False #リストが必要なときはこの2行を有効化
    for j in range(4,N,2): is_prime_list[j] = False
    m = int(N**0.5)+1
    for i in range(3,m,2):
        if is_prime_list[i]:
            is_prime_list[i*i::2*i]=[False]*((N-i*i-1)//(2*i)+1)
    #return [2] + [i for i in range(3,N,2) if is_prime_list[i]]
    return is_prime_list


res = [0]*10
n = int(input())
*a, = map(int,input().split())
for ai in a:
    res[ai] += 1

N = 5*10**6+1
ans = -1
lst = Eratosthenes(N)
used = [0]*10
r = 1
for l in range(1,N):
    if r < l:
        #print(l,r,used)
        if lst[r]:
            for i in str(r):
                used[int(i)] += 1
        r += 1
        #print(l,r,used)
        assert used == [0]*10
    while r < N:
        if lst[r]==1:
            flag = 1
            for i in str(r):
                if res[int(i)]==0: flag = 0
            if flag:
                for i in str(r):
                    used[int(i)] += 1
            else:
                break
        r += 1
    #print(l,r,used)
    for i in range(10):
        if (res[i] and used[i]==0) or (res[i]==0 and used[i]): break
    else:
        ans = max(ans,r-l-1)
        #print(l,r,used)
    #if l <= 20: print(l,r,used,"fin")
    if lst[l]:
        for i in str(l):
            used[int(i)] -= 1

print(ans)
0