結果

問題 No.1653 Squarefree
ユーザー gew1fw
提出日時 2025-06-12 16:49:06
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 3,026 bytes
コンパイル時間 353 ms
コンパイル使用メモリ 82,244 KB
実行使用メモリ 105,228 KB
最終ジャッジ日時 2025-06-12 16:50:12
合計ジャッジ時間 6,777 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample -- * 3
other TLE * 1 -- * 37
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import math
import random

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

def pollards_rho(n):
    if n % 2 == 0:
        return 2
    if n % 3 == 0:
        return 3
    if n % 5 == 0:
        return 5
    while True:
        c = random.randint(1, n-1)
        f = lambda x: (pow(x, 2, n) + c) % n
        x, y, d = 2, 2, 1
        while d == 1:
            x = f(x)
            y = f(f(y))
            d = math.gcd(abs(x - y), n)
        if d != n:
            return d

def factorize(n):
    factors = []
    def _factor(n):
        if n == 1:
            return
        if is_prime(n):
            factors.append(n)
            return
        d = pollards_rho(n)
        _factor(d)
        _factor(n // d)
    _factor(n)
    return factors

def main():
    L, R = map(int, sys.stdin.readline().split())
    
    # Precompute small primes up to 1e6 using sieve
    sieve_limit = 10**6
    sieve = [True] * (sieve_limit + 1)
    sieve[0] = sieve[1] = False
    for i in range(2, int(math.sqrt(sieve_limit)) + 1):
        if sieve[i]:
            for j in range(i*i, sieve_limit + 1, i):
                sieve[j] = False
    small_primes = [i for i, is_p in enumerate(sieve) if is_p]
    
    count = 0
    
    for n in range(L, R + 1):
        if n == 1:
            count += 1
            continue
        temp = n
        square_free = True
        
        # Check small primes
        for p in small_primes:
            if p * p > temp:
                break
            if temp % p == 0:
                if (temp // p) % p == 0:
                    square_free = False
                    break
                while temp % p == 0:
                    temp = temp // p
        if not square_free:
            continue
        
        if temp == 1:
            count += 1
            continue
        
        # Check if temp is a perfect square
        s = int(math.isqrt(temp))
        if s * s == temp:
            square_free = False
        else:
            # Check if temp is a prime
            if is_prime(temp):
                if n % (temp * temp) == 0:
                    square_free = False
            else:
                # Factor the remaining part
                factors = factorize(temp)
                for p in factors:
                    if n % (p * p) == 0:
                        square_free = False
                        break
        
        if square_free:
            count += 1
    
    print(count)

if __name__ == "__main__":
    main()
0