結果

問題 No.1661 Sum is Prime (Hard Version)
ユーザー tamatotamato
提出日時 2021-08-27 23:17:25
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,017 ms / 3,000 ms
コード長 2,948 bytes
コンパイル時間 274 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 87,552 KB
最終ジャッジ日時 2024-11-21 05:13:55
合計ジャッジ時間 9,536 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
52,864 KB
testcase_01 AC 38 ms
52,736 KB
testcase_02 AC 650 ms
81,792 KB
testcase_03 AC 38 ms
52,608 KB
testcase_04 AC 37 ms
52,864 KB
testcase_05 AC 37 ms
52,864 KB
testcase_06 AC 39 ms
53,248 KB
testcase_07 AC 37 ms
52,480 KB
testcase_08 AC 38 ms
52,992 KB
testcase_09 AC 39 ms
53,248 KB
testcase_10 AC 43 ms
59,648 KB
testcase_11 AC 39 ms
53,248 KB
testcase_12 AC 577 ms
81,024 KB
testcase_13 AC 563 ms
81,152 KB
testcase_14 AC 634 ms
83,456 KB
testcase_15 AC 817 ms
85,376 KB
testcase_16 AC 681 ms
84,088 KB
testcase_17 AC 630 ms
81,920 KB
testcase_18 AC 283 ms
73,472 KB
testcase_19 AC 473 ms
79,744 KB
testcase_20 AC 304 ms
75,008 KB
testcase_21 AC 533 ms
77,312 KB
testcase_22 AC 1,017 ms
87,552 KB
testcase_23 AC 984 ms
86,900 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