結果
問題 |
No.2954 Calculation of Exponentiation
|
ユーザー |
![]() |
提出日時 | 2025-03-31 17:20:43 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 41 ms / 2,000 ms |
コード長 | 1,634 bytes |
コンパイル時間 | 168 ms |
コンパイル使用メモリ | 81,968 KB |
実行使用メモリ | 54,860 KB |
最終ジャッジ日時 | 2025-03-31 17:22:03 |
合計ジャッジ時間 | 2,506 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 28 |
ソースコード
import math def parse_fraction(s, is_B): sign = 1 if is_B and s.startswith('-'): sign = -1 s = s[1:] parts = s.split('.') if len(parts) == 1: left = parts[0] right = '0000' else: left, right = parts right = right.ljust(4, '0')[:4] integer_part = int(left) fractional_part = int(right) numerator = integer_part * 10000 + fractional_part if is_B: numerator *= sign denominator = 10000 # Reduce numerator and denominator by their GCD g = math.gcd(abs(numerator), denominator) simplified_num = numerator // g simplified_den = denominator // g return (simplified_num, simplified_den) def is_kth_power(p, k): if p == 1: return True low = 1 high = p while low <= high: mid = (low + high) // 2 powered = mid ** k if powered == p: return True elif powered < p: low = mid + 1 else: high = mid - 1 return False a_str, b_str = input().split() # Parse A into numerator and denominator a_num, a_den = parse_fraction(a_str, False) p, q = a_num, a_den # Parse B into numerator and denominator b_num, b_den = parse_fraction(b_str, True) # Check if B is zero if b_num == 0: print("Yes") exit() # Handle negative B by swapping p and q if b_num < 0: p, q = q, p b_num = abs(b_num) r_abs, s = b_num, b_den g = math.gcd(r_abs, s) a = r_abs // g b_val = s // g # Check if the denominator is 1 and p is a perfect b_val-th power if q != 1: print("No") else: print("Yes" if is_kth_power(p, b_val) else "No")