結果

問題 No.1657 Sum is Prime (Easy Version)
ユーザー tktk_snsntktk_snsn
提出日時 2021-08-27 22:45:05
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 718 ms / 2,000 ms
コード長 611 bytes
コンパイル時間 98 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 97,376 KB
最終ジャッジ日時 2024-05-01 03:30:01
合計ジャッジ時間 11,980 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 372 ms
97,232 KB
testcase_01 AC 378 ms
97,152 KB
testcase_02 AC 648 ms
97,252 KB
testcase_03 AC 367 ms
97,324 KB
testcase_04 AC 369 ms
97,152 KB
testcase_05 AC 372 ms
97,304 KB
testcase_06 AC 374 ms
97,036 KB
testcase_07 AC 369 ms
97,036 KB
testcase_08 AC 381 ms
97,144 KB
testcase_09 AC 373 ms
97,252 KB
testcase_10 AC 372 ms
97,156 KB
testcase_11 AC 379 ms
97,104 KB
testcase_12 AC 616 ms
97,376 KB
testcase_13 AC 496 ms
97,032 KB
testcase_14 AC 500 ms
97,148 KB
testcase_15 AC 549 ms
97,204 KB
testcase_16 AC 427 ms
97,316 KB
testcase_17 AC 385 ms
97,140 KB
testcase_18 AC 394 ms
97,212 KB
testcase_19 AC 668 ms
97,164 KB
testcase_20 AC 451 ms
97,164 KB
testcase_21 AC 718 ms
97,316 KB
testcase_22 AC 384 ms
97,236 KB
testcase_23 AC 403 ms
97,208 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from itertools import chain


def prime_set(N):
    """
    Nまでの素数のsetを返す
    """
    if N < 4:
        return ({}, {}, {2}, {2, 3})[N]
    Nsq = int(N ** 0.5 + 0.5) + 1
    primes = {2, 3} | set(chain(range(5, N + 1, 6), range(7, N + 1, 6)))
    for i in range(5, Nsq, 2):
        if i in primes:
            primes -= set(range(i * i, N + 1, i * 2))
    return primes


U = 2 * 10 ** 6 + 10
Prime = prime_set(U)

L, R = map(int, input().split())
ans = 0
for i in range(L, R+1):
    if i in Prime:
        ans += 1
for i in range(L, R):
    if 2 * i + 1 in Prime:
        ans += 1
print(ans)
0