結果
| 問題 | No.1936 Rational Approximation |
| コンテスト | |
| ユーザー |
lam6er
|
| 提出日時 | 2025-03-20 20:46:23 |
| 言語 | PyPy3 (7.3.17) |
| 結果 |
AC
|
| 実行時間 | 56 ms / 2,000 ms |
| コード長 | 701 bytes |
| 記録 | |
| コンパイル時間 | 222 ms |
| コンパイル使用メモリ | 96,116 KB |
| 実行使用メモリ | 79,252 KB |
| 最終ジャッジ日時 | 2026-07-07 11:58:30 |
| 合計ジャッジ時間 | 2,292 ms |
|
ジャッジサーバーID (参考情報) |
judge3_0 / judge2_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 14 |
ソースコード
def extended_gcd(a, b):
if b == 0:
return (a, 1, 0)
else:
g, x, y = extended_gcd(b, a % b)
return (g, y, x - (a // b) * y)
def modinv(a, m):
g, x, y = extended_gcd(a, m)
if g != 1:
return None # This should not happen as per problem constraints
else:
return x % m
# Read input
P, Q = map(int, input().split())
# Compute inverse of P modulo Q
inv_p = modinv(P, Q)
# Calculate left fraction (a_left / b_left)
b_left = inv_p
a_left = (b_left * P - 1) // Q
# Calculate right fraction (c_right / d_right)
d_right = (-inv_p) % Q
c_right = (d_right * P + 1) // Q
# Sum all components
total = a_left + b_left + c_right + d_right
print(total)
lam6er