結果
| 問題 |
No.1653 Squarefree
|
| コンテスト | |
| ユーザー |
lam6er
|
| 提出日時 | 2025-04-16 15:41:27 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,639 bytes |
| コンパイル時間 | 360 ms |
| コンパイル使用メモリ | 82,004 KB |
| 実行使用メモリ | 301,528 KB |
| 最終ジャッジ日時 | 2025-04-16 15:45:15 |
| 合計ジャッジ時間 | 6,725 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | -- * 3 |
| other | TLE * 1 -- * 37 |
ソースコード
import math
def sieve_eratosthenes(n):
if n < 2:
return []
sieve = [True] * (n + 1)
sieve[0] = sieve[1] = False
for i in range(2, int(math.sqrt(n)) + 1):
if sieve[i]:
sieve[i*i : n+1 : i] = [False] * len(sieve[i*i : n+1 : i])
primes = [i for i, is_p in enumerate(sieve) if is_p]
return primes
def sieve_segmented(n):
if n < 2:
return []
sqrt_n = int(math.sqrt(n)) + 1
base_primes = sieve_eratosthenes(sqrt_n)
primes = []
segment_size = sqrt_n # Optimal segment size
low = 2
while low <= n:
high = min(low + segment_size - 1, n)
sieve = [True] * (high - low + 1)
for p in base_primes:
start = max(p * p, ((low + p - 1) // p) * p)
start = max(start, low)
for i in range(start, high + 1, p):
sieve[i - low] = False
for i in range(len(sieve)):
if sieve[i]:
primes.append(low + i)
low += segment_size
return primes
def count_square_free(L, R):
max_p = int(math.isqrt(R))
primes = sieve_segmented(max_p)
is_square_free = [True] * (R - L + 1)
for p in primes:
s = p * p
if s > R:
continue
# Find the first multiple of s >= L
start = ((L + s - 1) // s) * s
if start > R:
continue
# Mark all multiples of s in [L, R]
for multiple in range(start, R + 1, s):
idx = multiple - L
is_square_free[idx] = False
return sum(is_square_free)
# Read input
L, R = map(int, input().split())
print(count_square_free(L, R))
lam6er