結果

問題 No.1661 Sum is Prime (Hard Version)
ユーザー convexineqconvexineq
提出日時 2021-08-28 20:51:57
言語 PyPy3
(7.3.15)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 668 bytes
コンパイル時間 448 ms
コンパイル使用メモリ 87,160 KB
実行使用メモリ 106,724 KB
最終ジャッジ日時 2023-08-14 03:17:27
合計ジャッジ時間 26,746 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,528 KB
testcase_01 AC 72 ms
71,912 KB
testcase_02 AC 2,092 ms
97,388 KB
testcase_03 AC 71 ms
71,360 KB
testcase_04 AC 71 ms
72,000 KB
testcase_05 AC 71 ms
71,728 KB
testcase_06 AC 79 ms
75,964 KB
testcase_07 AC 73 ms
71,500 KB
testcase_08 AC 77 ms
76,204 KB
testcase_09 AC 78 ms
75,944 KB
testcase_10 AC 79 ms
76,340 KB
testcase_11 AC 77 ms
76,236 KB
testcase_12 AC 1,851 ms
95,908 KB
testcase_13 AC 1,797 ms
95,532 KB
testcase_14 AC 2,051 ms
97,876 KB
testcase_15 AC 2,693 ms
102,492 KB
testcase_16 AC 2,184 ms
98,436 KB
testcase_17 AC 2,035 ms
97,272 KB
testcase_18 AC 846 ms
85,500 KB
testcase_19 AC 1,483 ms
93,364 KB
testcase_20 AC 914 ms
87,168 KB
testcase_21 AC 1,734 ms
92,104 KB
testcase_22 AC 1,324 ms
89,132 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