結果
問題 |
No.376 立方体のN等分 (2)
|
ユーザー |
![]() |
提出日時 | 2025-03-20 20:45:16 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,157 bytes |
コンパイル時間 | 421 ms |
コンパイル使用メモリ | 82,236 KB |
実行使用メモリ | 78,416 KB |
最終ジャッジ日時 | 2025-03-20 20:45:27 |
合計ジャッジ時間 | 3,934 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 22 WA * 16 |
ソースコード
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 factor(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 find_min_sum(m): if m == 1: return 2 if is_prime(m): return 1 + m for d in factor(m): if d > 1: divisor = d break other = m // divisor q_max = other if q_max > math.isqrt(m): q_max = m // q_max return q_max + (m // q_max) def solve(): N = int(input().strip()) if N == 1: print("0 0") return tmax = N - 1 cube_root = int(N ** (1/3)) + 2 min_sum = float('inf') for p in range(1, cube_root + 1): if N % p != 0: continue m = N // p if m == 0: continue current = p + find_min_sum(m) if current < min_sum: min_sum = current if min_sum == float('inf'): tmin = N - 1 else: tmin = min_sum - 3 print(f"{tmin} {tmax}") if __name__ == "__main__": solve()