結果

問題 No.1936 Rational Approximation
ユーザー sotanishysotanishy
提出日時 2022-05-13 23:18:07
言語 PyPy3
(7.3.8)
結果
AC  
実行時間 83 ms / 2,000 ms
コード長 587 bytes
コンパイル時間 264 ms
使用メモリ 75,936 KB
最終ジャッジ日時 2023-02-21 17:12:51
合計ジャッジ時間 2,564 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 81 ms
75,808 KB
testcase_01 AC 81 ms
75,812 KB
testcase_02 AC 83 ms
75,576 KB
testcase_03 AC 81 ms
75,768 KB
testcase_04 AC 82 ms
75,672 KB
testcase_05 AC 82 ms
75,904 KB
testcase_06 AC 81 ms
75,884 KB
testcase_07 AC 81 ms
75,836 KB
testcase_08 AC 80 ms
75,636 KB
testcase_09 AC 81 ms
75,768 KB
testcase_10 AC 83 ms
75,936 KB
testcase_11 AC 83 ms
75,724 KB
testcase_12 AC 83 ms
75,580 KB
testcase_13 AC 80 ms
75,576 KB
testcase_14 AC 82 ms
75,752 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