結果

問題 No.12 限定された素数
ユーザー しらっ亭
提出日時 2015-07-10 20:43:06
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 1,606 ms / 5,000 ms
コード長 1,131 bytes
コンパイル時間 329 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 63,736 KB
最終ジャッジ日時 2024-11-24 08:27:51
合計ジャッジ時間 44,125 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

def mark(s, x):
    for i in range(x + x, len(s), x):
        s[i] = False

def sieve(n):
    s = [True] * n
    for x in range(2, int(n**0.5) + 1):
        if s[x]:
            mark(s, x)
    return [i for i in range(n) if s[i] and i > 1]


def solve(A):
    MAX = 5000000
    P = sieve(MAX)

    B = sum(1 << a for a in A)

    b2 = {str(c): 1 << c for c in range(10)}
    M = [sum(b2[c] for c in set(str(p))) for p in P]

    l = r = ps = s = 0

    ans = -1

    while r < len(M):
        sb = s & B
        if s - sb:
            if ps == B:
                if r == len(M) - 1:
                    r1 = MAX
                else:
                    r1 = P[r - 1] - 1
                if l == 0:
                    l1 = 1
                else:
                    l1 = P[l - 1] + 1
                ans = max(ans, r1 - l1)
            l = r
            s = 0
        else:
            ps = s
            s = ps | M[r]
            r += 1

    if ps and ans == -1:
        return 4999999

    return ans


def main():
    input()
    A = list(map(int, input().split()))

    print(solve(A))


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