結果

問題 No.1661 Sum is Prime (Hard Version)
ユーザー convexineqconvexineq
提出日時 2021-08-28 20:55:07
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,758 ms / 3,000 ms
コード長 667 bytes
コンパイル時間 454 ms
コンパイル使用メモリ 87,096 KB
実行使用メモリ 106,440 KB
最終ジャッジ日時 2023-08-14 03:18:20
合計ジャッジ時間 22,606 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 68 ms
71,636 KB
testcase_01 AC 62 ms
71,700 KB
testcase_02 AC 1,771 ms
97,032 KB
testcase_03 AC 62 ms
71,308 KB
testcase_04 AC 57 ms
71,832 KB
testcase_05 AC 57 ms
71,652 KB
testcase_06 AC 62 ms
76,004 KB
testcase_07 AC 58 ms
71,756 KB
testcase_08 AC 61 ms
75,792 KB
testcase_09 AC 63 ms
75,996 KB
testcase_10 AC 65 ms
75,892 KB
testcase_11 AC 61 ms
75,948 KB
testcase_12 AC 1,566 ms
95,760 KB
testcase_13 AC 1,534 ms
95,344 KB
testcase_14 AC 1,775 ms
97,560 KB
testcase_15 AC 2,268 ms
102,360 KB
testcase_16 AC 1,849 ms
98,596 KB
testcase_17 AC 1,714 ms
96,972 KB
testcase_18 AC 717 ms
85,460 KB
testcase_19 AC 1,258 ms
93,008 KB
testcase_20 AC 785 ms
87,132 KB
testcase_21 AC 1,465 ms
91,928 KB
testcase_22 AC 1,144 ms
89,284 KB
testcase_23 AC 2,758 ms
106,440 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def prime_counting(n):
    if n <= 1: return 0
    r = int(n**0.5)
    assert r*r <= n < (r+1)**2
    V = [0] + [n//i for i in range(1,r+1)] + list(range(n//r-1,0,-1))
    S = [i-1 for i in V]
    for p in range(2,r+1):
        if S[-p] > S[-p+1]:  # p is prime
            sp = S[-p+1]  # number of primes smaller than p
            p2 = p*p
            for i in range(1,2*r+1):
                v = V[i]
                if v < p2: break
                S[i] -= (S[-(v//p) if v//p <= r else i*p] - sp)
    return S[1]

L,R = map(int,input().split())
ans = prime_counting(R) - prime_counting(L-1)
if L < R: ans += prime_counting(2*R-1) - prime_counting(2*L)
print(ans)
0