結果

問題 No.1661 Sum is Prime (Hard Version)
ユーザー tamatotamato
提出日時 2021-08-27 23:15:58
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,988 bytes
コンパイル時間 227 ms
コンパイル使用メモリ 82,216 KB
実行使用メモリ 87,048 KB
最終ジャッジ日時 2024-11-21 05:11:30
合計ジャッジ時間 7,611 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 40 ms
52,352 KB
testcase_02 WA -
testcase_03 AC 39 ms
52,608 KB
testcase_04 WA -
testcase_05 AC 39 ms
53,120 KB
testcase_06 AC 39 ms
53,248 KB
testcase_07 WA -
testcase_08 AC 40 ms
53,120 KB
testcase_09 AC 39 ms
53,248 KB
testcase_10 AC 47 ms
59,776 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 819 ms
85,504 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 1,022 ms
87,048 KB
testcase_23 AC 984 ms
86,960 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))
    exit()
if 2 * L + 1 > R:
    ans = pi(R) - pi(L-1) + pi(2*R-1) - pi(2*L)
else:
    ans = pi(2*R-1) - pi(L-1)
print(ans)
0