結果

問題 No.1661 Sum is Prime (Hard Version)
ユーザー 👑 tamatotamato
提出日時 2021-08-27 23:17:25
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,026 ms / 3,000 ms
コード長 2,948 bytes
コンパイル時間 2,325 ms
コンパイル使用メモリ 86,704 KB
実行使用メモリ 85,100 KB
最終ジャッジ日時 2023-08-13 10:52:03
合計ジャッジ時間 10,596 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,740 KB
testcase_01 AC 77 ms
71,796 KB
testcase_02 AC 660 ms
83,512 KB
testcase_03 AC 73 ms
71,000 KB
testcase_04 AC 76 ms
71,848 KB
testcase_05 AC 73 ms
71,548 KB
testcase_06 AC 75 ms
71,596 KB
testcase_07 AC 75 ms
71,688 KB
testcase_08 AC 74 ms
71,800 KB
testcase_09 AC 73 ms
71,760 KB
testcase_10 AC 79 ms
76,508 KB
testcase_11 AC 73 ms
71,552 KB
testcase_12 AC 589 ms
83,032 KB
testcase_13 AC 578 ms
82,656 KB
testcase_14 AC 645 ms
82,920 KB
testcase_15 AC 821 ms
84,252 KB
testcase_16 AC 685 ms
83,556 KB
testcase_17 AC 639 ms
83,620 KB
testcase_18 AC 307 ms
80,064 KB
testcase_19 AC 493 ms
83,456 KB
testcase_20 AC 326 ms
80,732 KB
testcase_21 AC 549 ms
82,532 KB
testcase_22 AC 1,026 ms
85,080 KB
testcase_23 AC 982 ms
85,100 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def count_primes(n):
    r = int(n ** 0.5)
    assert r * r <= n and (r + 1) ** 2 > n
    V = [0] + [n // i for i in range(1, r + 1)]
    V += list(range(V[-1] - 1, 0, -1))
    S = [i - 1 for i in V]
    for p in range(2, r + 1):
        if S[-p] > S[-p + 1]:
            sp = S[-p + 1]
            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]

def faster_count_primes(n):
    v = int(n ** 0.5)
    higher = [0] * (v + 2)
    lower  = [0] * (v + 2)
    used   = [False] * (v + 2)
    result = n - 1
    for p in range(2, v + 1):
        lower[p] = p - 1
        higher[p] = n // p - 1
    for p in range(2, v + 1):
        if lower[p] == lower[p - 1]:
            continue
        temp = lower[p - 1]
        result -= higher[p] - temp
        pxp = p * p
        end = min(v, n // pxp)
        j = 1 + (p & 1)
        for i in range(p + j, end + 2, j):
            if used[i]:
                continue
            d = i * p
            if d <= v:
                higher[i] -= higher[d] - temp
            else:
                higher[i] -= lower[n // d] - temp
        for i in range(v, pxp - 1, -1):
            lower[i] -= lower[i // p] - temp
        for i in range(pxp, end + 1, p * j):
            used[i] = True
    return result

def fastest_count_primes(n):
    if n < 2:
        return 0
    v = int(n ** 0.5) + 1
    smalls = [i // 2 for i in range(1, v + 1)]
    smalls[1] = 0
    s = v // 2
    roughs = [2 * i + 1 for i in range(s)]
    larges = [(n // (2 * i + 1) + 1) // 2 for i in range(s)]
    skip = [False] * v

    pc = 0
    for p in range(3, v):
        if smalls[p] <= smalls[p - 1]:
            continue

        q = p * p
        pc += 1
        if q * q > n:
            break
        skip[p] = True
        for i in range(q, v, 2 * p):
            skip[i] = True

        ns = 0
        for k in range(s):
            i = roughs[k]
            if skip[i]:
                continue
            d = i * p
            larges[ns] = larges[k] - (larges[smalls[d] - pc] if d < v else smalls[n // d]) + pc
            roughs[ns] = i
            ns += 1
        s = ns
        for j in range((v - 1) // p, p - 1, -1):
            c = smalls[j] - pc
            e = min((j + 1) * p, v)
            for i in range(j * p, e):
                smalls[i] -= c

    for k in range(1, s):
        m = n // roughs[k]
        s = larges[k] - (pc + k - 1)
        for l in range(1, k):
            p = roughs[l]
            if p * p > m:
                break
            s -= smalls[m // p] - (pc + l - 1)
        larges[0] -= s

    return larges[0]


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