結果

問題 No.1661 Sum is Prime (Hard Version)
ユーザー convexineqconvexineq
提出日時 2021-08-28 20:51:57
言語 PyPy3
(7.3.15)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 668 bytes
コンパイル時間 354 ms
コンパイル使用メモリ 82,196 KB
実行使用メモリ 102,180 KB
最終ジャッジ日時 2024-05-01 16:08:31
合計ジャッジ時間 24,733 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
53,812 KB
testcase_01 AC 36 ms
52,676 KB
testcase_02 AC 2,032 ms
90,156 KB
testcase_03 AC 36 ms
53,240 KB
testcase_04 AC 35 ms
53,440 KB
testcase_05 AC 36 ms
53,024 KB
testcase_06 AC 41 ms
59,860 KB
testcase_07 AC 36 ms
52,700 KB
testcase_08 AC 38 ms
58,972 KB
testcase_09 AC 42 ms
59,020 KB
testcase_10 AC 43 ms
58,988 KB
testcase_11 AC 42 ms
59,336 KB
testcase_12 AC 1,784 ms
88,772 KB
testcase_13 AC 1,738 ms
88,520 KB
testcase_14 AC 1,966 ms
90,052 KB
testcase_15 AC 2,549 ms
95,768 KB
testcase_16 AC 2,083 ms
90,956 KB
testcase_17 AC 1,937 ms
89,836 KB
testcase_18 AC 805 ms
76,920 KB
testcase_19 AC 1,449 ms
86,320 KB
testcase_20 AC 862 ms
79,448 KB
testcase_21 AC 1,644 ms
84,924 KB
testcase_22 AC 1,252 ms
78,720 KB
testcase_23 TLE -
権限があれば一括ダウンロードができます

ソースコード

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