結果

問題 No.375 立方体のN等分 (1)
ユーザー lam6er
提出日時 2025-04-16 15:52:32
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 139 ms / 5,000 ms
コード長 2,195 bytes
コンパイル時間 298 ms
コンパイル使用メモリ 81,764 KB
実行使用メモリ 76,332 KB
最終ジャッジ日時 2025-04-16 15:53:43
合計ジャッジ時間 2,707 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

import math

def factorize(n):
    factors = {}
    while n % 2 == 0:
        factors[2] = factors.get(2, 0) + 1
        n = n // 2
    i = 3
    while i * i <= n:
        while n % i == 0:
            factors[i] = factors.get(i, 0) + 1
            n = n // i
        i += 2
    if n > 1:
        factors[n] = 1
    return factors

def generate_divisors(factors):
    divisors = [1]
    for p, exp in factors.items():
        current_length = len(divisors)
        for e in range(1, exp + 1):
            pe = p ** e
            for i in range(current_length):
                divisors.append(divisors[i] * pe)
    divisors = list(set(divisors))
    divisors.sort()
    return divisors

def get_factors_a(a, original_factors):
    factors_a = {}
    for p in original_factors:
        count = 0
        temp = a
        while temp % p == 0 and temp > 0:
            count += 1
            temp = temp // p
        if count > 0:
            factors_a[p] = count
    return factors_a

def subtract_factors(original_factors, factors_a):
    factors_m = {}
    for p in original_factors:
        exp = original_factors[p] - factors_a.get(p, 0)
        if exp > 0:
            factors_m[p] = exp
    return factors_m

def main():
    N = int(input().strip())
    if N == 1:
        print("0 0")
        return
    
    factors = factorize(N)
    divisors = generate_divisors(factors)
    
    a_candidates = []
    for a in divisors:
        if a ** 3 <= N:
            a_candidates.append(a)
    
    min_sum = float('inf')
    
    for a in a_candidates:
        M = N // a
        factors_a = get_factors_a(a, factors)
        factors_m = subtract_factors(factors, factors_a)
        divisors_m = generate_divisors(factors_m)
        sqrt_m = int(math.isqrt(M))
        for b in divisors_m:
            if b < a or b > sqrt_m:
                continue
            if M % b != 0:
                continue
            c = M // b
            if c < b:
                continue
            current_sum = a + b + c
            if current_sum < min_sum:
                min_sum = current_sum
    
    Tmin = min_sum - 3
    Tmax = N - 1
    print(f"{Tmin} {Tmax}")

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