結果

問題 No.1936 Rational Approximation
ユーザー sotanishysotanishy
提出日時 2022-05-13 23:18:07
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 71 ms / 2,000 ms
コード長 587 bytes
コンパイル時間 526 ms
コンパイル使用メモリ 86,824 KB
実行使用メモリ 71,476 KB
最終ジャッジ日時 2023-09-29 08:58:32
合計ジャッジ時間 2,323 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
71,476 KB
testcase_01 AC 71 ms
71,464 KB
testcase_02 AC 71 ms
71,256 KB
testcase_03 AC 71 ms
71,176 KB
testcase_04 AC 70 ms
71,460 KB
testcase_05 AC 70 ms
71,116 KB
testcase_06 AC 70 ms
71,328 KB
testcase_07 AC 71 ms
71,436 KB
testcase_08 AC 71 ms
71,388 KB
testcase_09 AC 71 ms
71,364 KB
testcase_10 AC 70 ms
71,340 KB
testcase_11 AC 70 ms
71,288 KB
testcase_12 AC 70 ms
71,408 KB
testcase_13 AC 69 ms
71,120 KB
testcase_14 AC 70 ms
71,360 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from math import gcd
import sys
input = sys.stdin.readline

def extgcd(a, b):
    s, sx, sy, t, tx, ty = a, 1, 0, b, 0, 1
    while t:
        q = s // t
        s -= t * q
        s, t = t, s
        sx -= tx * q
        sx, tx = tx, sx
        sy -= ty * q
        sy, ty = ty, sy
    return sx, sy

P, Q = map(int, input().split())

def calc(P, Q):
    y, x = extgcd(P, Q)
    if y < 0:
        z = (-y + Q - 1) // Q
        y += z*Q
        x -= z*P
    x *= -1
    g = gcd(x, y)
    return x//g, y//g

pl, ql = calc(P, Q)
pr, qr = calc(Q-P, Q)
pr = qr - pr
print(pl + ql + pr + qr)
0