結果
問題 | No.1661 Sum is Prime (Hard Version) |
ユーザー | mkawa2 |
提出日時 | 2023-11-06 16:16:04 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,343 bytes |
コンパイル時間 | 146 ms |
コンパイル使用メモリ | 82,376 KB |
実行使用メモリ | 85,544 KB |
最終ジャッジ日時 | 2024-09-25 23:04:05 |
合計ジャッジ時間 | 3,802 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 33 ms
52,480 KB |
testcase_01 | AC | 35 ms
52,480 KB |
testcase_02 | AC | 183 ms
82,664 KB |
testcase_03 | WA | - |
testcase_04 | AC | 33 ms
52,864 KB |
testcase_05 | AC | 33 ms
52,992 KB |
testcase_06 | AC | 33 ms
52,864 KB |
testcase_07 | AC | 35 ms
52,864 KB |
testcase_08 | AC | 36 ms
53,248 KB |
testcase_09 | AC | 33 ms
52,864 KB |
testcase_10 | AC | 34 ms
53,120 KB |
testcase_11 | AC | 34 ms
53,376 KB |
testcase_12 | AC | 159 ms
81,492 KB |
testcase_13 | AC | 158 ms
82,708 KB |
testcase_14 | AC | 224 ms
82,560 KB |
testcase_15 | AC | 203 ms
83,100 KB |
testcase_16 | AC | 190 ms
82,628 KB |
testcase_17 | AC | 175 ms
81,996 KB |
testcase_18 | AC | 107 ms
79,616 KB |
testcase_19 | AC | 150 ms
81,972 KB |
testcase_20 | AC | 118 ms
78,816 KB |
testcase_21 | AC | 195 ms
82,432 KB |
testcase_22 | AC | 331 ms
85,544 KB |
testcase_23 | AC | 241 ms
84,308 KB |
ソースコード
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)