結果
問題 | No.1661 Sum is Prime (Hard Version) |
ユーザー | 👑 rin204 |
提出日時 | 2022-01-25 18:55:09 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 405 ms / 3,000 ms |
コード長 | 1,558 bytes |
コンパイル時間 | 220 ms |
コンパイル使用メモリ | 82,576 KB |
実行使用メモリ | 87,504 KB |
最終ジャッジ日時 | 2024-05-09 19:51:27 |
合計ジャッジ時間 | 4,865 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 37 ms
52,740 KB |
testcase_01 | AC | 37 ms
53,332 KB |
testcase_02 | AC | 322 ms
83,124 KB |
testcase_03 | AC | 38 ms
52,728 KB |
testcase_04 | AC | 38 ms
53,300 KB |
testcase_05 | AC | 37 ms
53,268 KB |
testcase_06 | AC | 37 ms
53,244 KB |
testcase_07 | AC | 38 ms
54,112 KB |
testcase_08 | AC | 37 ms
53,864 KB |
testcase_09 | AC | 37 ms
53,064 KB |
testcase_10 | AC | 37 ms
54,140 KB |
testcase_11 | AC | 36 ms
53,084 KB |
testcase_12 | AC | 230 ms
82,792 KB |
testcase_13 | AC | 225 ms
82,408 KB |
testcase_14 | AC | 278 ms
84,440 KB |
testcase_15 | AC | 294 ms
84,236 KB |
testcase_16 | AC | 251 ms
83,304 KB |
testcase_17 | AC | 239 ms
83,044 KB |
testcase_18 | AC | 166 ms
80,144 KB |
testcase_19 | AC | 244 ms
81,364 KB |
testcase_20 | AC | 174 ms
80,408 KB |
testcase_21 | AC | 239 ms
81,836 KB |
testcase_22 | AC | 405 ms
87,504 KB |
testcase_23 | AC | 340 ms
87,464 KB |
ソースコード
# https://judge.yosupo.jp/submission/69031 def 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 l == r == 1: print(0) exit() ans = count_primes(r) - count_primes(l - 1) ans += count_primes(2 * r - 1) - count_primes(2 * l) print(ans)