結果

問題 No.12 限定された素数
ユーザー convexineqconvexineq
提出日時 2021-01-05 09:48:00
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,351 bytes
コンパイル時間 177 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 142,364 KB
最終ジャッジ日時 2024-04-23 16:08:23
合計ジャッジ時間 18,251 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 489 ms
142,080 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 478 ms
142,152 KB
testcase_18 AC 545 ms
142,128 KB
testcase_19 AC 540 ms
142,268 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
権限があれば一括ダウンロードができます

ソースコード

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 and lst[r]:
        for i in str(r):
            used[int(i)] += 1
        #assert used == [0]*10
        r += 1
    while r < N:
        if lst[r+1]==1:
            flag = 1
            for i in str(r+1):
                if res[int(i)]==0: flag = 0
            if flag:
                for i in str(r+1):
                    used[int(i)] += 1
            else:
                break
        r += 1
    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)
        #print(l,r,used)
    #if l <= 20: print(l,r,used)
    if lst[l]:
        for i in str(l):
            used[int(i)] -= 1

print(ans)
0