結果

問題 No.1653 Squarefree
ユーザー lam6er
提出日時 2025-04-16 15:39:17
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,231 bytes
コンパイル時間 267 ms
コンパイル使用メモリ 82,152 KB
実行使用メモリ 100,328 KB
最終ジャッジ日時 2025-04-16 15:44:23
合計ジャッジ時間 6,681 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample -- * 3
other TLE * 1 -- * 37
権限があれば一括ダウンロードができます

ソースコード

diff #

import math

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())
marked = [False] * (R - L + 1)

max_d = int(math.isqrt(R))
for d in range(2, max_d + 1):
    if is_prime(d):
        d_squared = d * d
        start = ((L + d_squared - 1) // d_squared) * d_squared
        if start > R:
            continue
        # Mark all multiples of d_squared in [L, R]
        for x in range(start, R + 1, d_squared):
            marked[x - L] = True

# Check for numbers that are squares of primes larger than max_d
for x in range(L, R + 1):
    s = int(math.isqrt(x))
    if s * s == x and is_prime(s):
        marked[x - L] = True

count = len(marked) - sum(marked)
print(count)
0