結果

問題 No.1657 Sum is Prime (Easy Version)
ユーザー tktk_snsntktk_snsn
提出日時 2021-08-27 22:45:05
言語 Python3
(3.11.6 + numpy 1.26.0 + scipy 1.11.3)
結果
AC  
実行時間 619 ms / 2,000 ms
コード長 611 bytes
コンパイル時間 125 ms
コンパイル使用メモリ 10,752 KB
実行使用メモリ 94,928 KB
最終ジャッジ日時 2023-08-13 10:02:22
合計ジャッジ時間 11,401 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 349 ms
94,728 KB
testcase_01 AC 347 ms
94,784 KB
testcase_02 AC 573 ms
94,824 KB
testcase_03 AC 348 ms
94,864 KB
testcase_04 AC 344 ms
94,728 KB
testcase_05 AC 338 ms
94,736 KB
testcase_06 AC 357 ms
94,760 KB
testcase_07 AC 344 ms
94,772 KB
testcase_08 AC 362 ms
94,928 KB
testcase_09 AC 351 ms
94,732 KB
testcase_10 AC 349 ms
94,824 KB
testcase_11 AC 357 ms
94,812 KB
testcase_12 AC 556 ms
94,772 KB
testcase_13 AC 439 ms
94,920 KB
testcase_14 AC 446 ms
94,744 KB
testcase_15 AC 484 ms
94,824 KB
testcase_16 AC 391 ms
94,740 KB
testcase_17 AC 361 ms
94,728 KB
testcase_18 AC 364 ms
94,796 KB
testcase_19 AC 590 ms
94,768 KB
testcase_20 AC 410 ms
94,784 KB
testcase_21 AC 619 ms
94,772 KB
testcase_22 AC 343 ms
94,928 KB
testcase_23 AC 361 ms
94,768 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