結果

問題 No.375 立方体のN等分 (1)
ユーザー lam6er
提出日時 2025-04-15 22:39:11
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 115 ms / 5,000 ms
コード長 2,576 bytes
コンパイル時間 431 ms
コンパイル使用メモリ 82,332 KB
実行使用メモリ 76,880 KB
最終ジャッジ日時 2025-04-15 22:40:46
合計ジャッジ時間 2,992 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
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 = []
        for e in range(exp + 1):
            pe = p ** e
            for d in divisors:
                current.append(d * pe)
        divisors = current
    return sorted(divisors)

def integer_cube_root(n):
    low = 1
    high = n
    while low <= high:
        mid = (low + high) // 2
        if mid ** 3 == n:
            return mid
        elif mid ** 3 < n:
            low = mid + 1
        else:
            high = mid - 1
    return high

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

def get_m_factors(factors_n, factors_a):
    factors_m = {}
    for p in factors_n:
        exp = factors_n[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_n = factorize(n)
    divisors_n = generate_divisors(factors_n)
    cube_root_n = integer_cube_root(n)
    a_candidates = [a for a in divisors_n if a <= cube_root_n and a < n]

    min_sum = float('inf')

    for a in a_candidates:
        factors_a = get_prime_factors(a, factors_n)
        factors_m = get_m_factors(factors_n, factors_a)
        if not factors_m:
            current_sum = a + 1 + 1
            if current_sum < min_sum:
                min_sum = current_sum
            continue
        m = n // a
        divisors_m = generate_divisors(factors_m)
        sqrt_m = int(math.isqrt(m))
        max_b = 0
        for b in reversed(divisors_m):
            if b >= a and b <= sqrt_m:
                max_b = b
                break
        if max_b == 0:
            continue
        c = m // max_b
        if max_b > c:
            continue
        current_sum = a + max_b + c
        if current_sum < min_sum:
            min_sum = current_sum

    if min_sum == float('inf'):
        tmin = n - 1
    else:
        tmin = min_sum - 3
    tmax = n - 1

    print(tmin, tmax)

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