結果

問題 No.1322 Totient Bound
ユーザー lam6er
提出日時 2025-03-31 17:53:17
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,641 bytes
コンパイル時間 149 ms
コンパイル使用メモリ 82,360 KB
実行使用メモリ 67,048 KB
最終ジャッジ日時 2025-03-31 17:54:39
合計ジャッジ時間 7,550 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 10 TLE * 1 -- * 25
権限があれば一括ダウンロードができます

ソースコード

diff #

def is_prime(n):
    if n <= 1:
        return False
    elif n <= 3:
        return True
    elif n % 2 == 0:
        return False
    d = n - 1
    s = 0
    while d % 2 == 0:
        d //= 2
        s += 1
    bases = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]
    for a in bases:
        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 generate_primes(start, end):
    primes = []
    for num in range(start, end + 1):
        if num >= 2 and is_prime(num):
            primes.append(num)
    return primes

def solve(N):
    if N < 1:
        return 0
    count = 0

    def backtrack(current_product, min_p, current_x):
        nonlocal count
        if current_product > N:
            return
        count += 1
        R = N // current_product
        start = min_p
        end = R + 1
        if start > end:
            return
        primes = generate_primes(start, end)
        for p in primes:
            if (p - 1) > R:
                continue
            e = 1
            while True:
                contribution = (p - 1) * (p) ** (e - 1)
                new_product = current_product * contribution
                if new_product > N:
                    break
                new_x = current_x * (p ** e)
                backtrack(new_product, p + 1, new_x)
                e += 1

    backtrack(1, 2, 1)
    return count

# Read input and output result
N = int(input())
print(solve(N))
0