結果

問題 No.1661 Sum is Prime (Hard Version)
ユーザー mkawa2mkawa2
提出日時 2023-11-06 16:16:04
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,343 bytes
コンパイル時間 418 ms
コンパイル使用メモリ 81,848 KB
実行使用メモリ 85,276 KB
最終ジャッジ日時 2023-11-06 16:16:10
合計ジャッジ時間 5,529 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,888 KB
testcase_01 AC 37 ms
53,952 KB
testcase_02 AC 192 ms
82,060 KB
testcase_03 WA -
testcase_04 AC 38 ms
53,952 KB
testcase_05 AC 37 ms
53,952 KB
testcase_06 AC 38 ms
54,016 KB
testcase_07 AC 38 ms
54,016 KB
testcase_08 AC 38 ms
54,016 KB
testcase_09 AC 38 ms
54,016 KB
testcase_10 AC 38 ms
54,016 KB
testcase_11 AC 38 ms
54,016 KB
testcase_12 AC 205 ms
81,356 KB
testcase_13 AC 177 ms
82,400 KB
testcase_14 AC 242 ms
82,248 KB
testcase_15 AC 227 ms
82,864 KB
testcase_16 AC 197 ms
81,968 KB
testcase_17 AC 187 ms
81,848 KB
testcase_18 AC 145 ms
79,608 KB
testcase_19 AC 159 ms
81,676 KB
testcase_20 AC 122 ms
78,564 KB
testcase_21 AC 211 ms
82,248 KB
testcase_22 AC 356 ms
85,276 KB
testcase_23 AC 287 ms
83,872 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(1000005)
# sys.set_int_max_str_digits(200005)
int1 = lambda x: int(x)-1
pDB = lambda *x: print(*x, end="\n", file=sys.stderr)
p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr)
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()

# dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
# inf = -1-(-1 << 31)
inf = -1-(-1 << 63)
# md = 10**9+7
md = 998244353

# Thanks for https://judge.yosupo.jp/submission/126093
def prime_counting(n: int):
    if n == 2: return 1
    if n == 3: return 2
    if n < 2: return 0
    v = int(n**0.5)-1
    while v*v <= n: v += 1
    v -= 1
    smalls = [i+1 >> 1 for i in range(v+1)]
    s = v+1 >> 1
    roughs = [i << 1 | 1 for i in range(s)]
    larges = [int(n/(i << 1 | 1)+1) >> 1 for i in range(s)]
    skip = bytearray([0]*(v+1))
    pc = 0
    for p in range(3, v+1, 2):
        if skip[p]: continue
        q = p*p
        pc += 1
        if q*q > n: break
        skip[p] = 1
        for i in range(q, v+1, p << 1): skip[i] = 1
        ns = 0
        for k in range(s):
            i = roughs[k]
            if skip[i]:
                continue
            d = i*p
            if d <= v:
                x = larges[smalls[d]-pc]
            else:
                x = smalls[int(n/d)]
            larges[ns] = larges[k]+pc-x
            roughs[ns] = i
            ns += 1
        s = ns
        i = v
        for j in range(int(v/p), p-1, -1):
            c = smalls[j]-pc
            e = j*p
            while i >= e:
                smalls[i] -= c
                i -= 1
    ret = larges[0]+((s+(pc-1 << 1))*(s-1) >> 1)-sum(larges[1:s])

    for l in range(1, s):
        q = roughs[l]
        m = int(n/q)
        e = smalls[int(m/q)]-pc
        if e <= l: break
        t = 0
        for r in roughs[l+1:e+1]: t += smalls[int(m/r)]
        ret += t-(e-l)*(pc+l-1)
    return ret

l, r = LI()
ans = prime_counting(r)-prime_counting(l-1)+prime_counting(2*r-1)-prime_counting(l*2)
print(ans)
0