結果

問題 No.1653 Squarefree
ユーザー gew1fw
提出日時 2025-06-12 18:36:46
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,576 bytes
コンパイル時間 175 ms
コンパイル使用メモリ 82,260 KB
実行使用メモリ 122,868 KB
最終ジャッジ日時 2025-06-12 18:37:11
合計ジャッジ時間 12,245 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 35 WA * 3
権限があれば一括ダウンロードができます

ソースコード

diff #

import math

def sieve(n):
    sieve = [True] * (n + 1)
    sieve[0] = sieve[1] = False
    for i in range(2, int(math.isqrt(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 is_prime(n):
    if n < 2:
        return False
    for p in [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]:
        if n % p == 0:
            return n == p
    d = n - 1
    s = 0
    while d % 2 == 0:
        d //= 2
        s += 1
    for a in [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]:
        if a >= n:
            continue
        x = pow(a, d, n)
        if x == 1 or x == n - 1:
            continue
        for _ in range(s - 1):
            x = pow(x, 2, n)
            if x == n - 1:
                break
        else:
            return False
    return True

L, R = map(int, input().split())
primes = sieve(10**6)

is_square_free = [True] * (R - L + 1)

for p in primes:
    p_squared = p * p
    if p_squared > R:
        continue
    first = ((L + p_squared - 1) // p_squared) * p_squared
    if first > R:
        continue
    start_idx = first - L
    step = p_squared
    for idx in range(start_idx, len(is_square_free), step):
        if idx >= 0:
            is_square_free[idx] = False

for i in range(len(is_square_free)):
    if not is_square_free[i]:
        continue
    x = L + i
    sqrt_x = math.isqrt(x)
    if sqrt_x * sqrt_x != x:
        continue
    if is_prime(sqrt_x):
        is_square_free[i] = False

print(sum(is_square_free))
0