結果
問題 | No.1661 Sum is Prime (Hard Version) |
ユーザー | tamato |
提出日時 | 2021-08-27 23:13:39 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,941 bytes |
コンパイル時間 | 328 ms |
コンパイル使用メモリ | 82,468 KB |
実行使用メモリ | 87,244 KB |
最終ジャッジ日時 | 2024-11-21 05:07:10 |
合計ジャッジ時間 | 7,648 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 36 ms
53,672 KB |
testcase_01 | AC | 36 ms
53,344 KB |
testcase_02 | WA | - |
testcase_03 | AC | 36 ms
52,976 KB |
testcase_04 | WA | - |
testcase_05 | AC | 37 ms
53,936 KB |
testcase_06 | AC | 36 ms
53,792 KB |
testcase_07 | WA | - |
testcase_08 | AC | 37 ms
54,956 KB |
testcase_09 | AC | 37 ms
53,328 KB |
testcase_10 | AC | 41 ms
60,396 KB |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | AC | 813 ms
85,264 KB |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | AC | 1,016 ms
87,244 KB |
testcase_23 | AC | 981 ms
87,084 KB |
ソースコード
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 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)