結果

問題 No.1936 Rational Approximation
ユーザー Akijin_007
提出日時 2022-05-13 22:55:06
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 40 ms / 2,000 ms
コード長 503 bytes
コンパイル時間 268 ms
コンパイル使用メモリ 82,360 KB
実行使用メモリ 53,472 KB
最終ジャッジ日時 2024-07-22 03:10:18
合計ジャッジ時間 1,524 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 14
権限があれば一括ダウンロードができます

ソースコード

diff #

#int(input())
#map(int, input().split())
#list(map(int, input().split()))

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

def xgcd(a,b):
    prevx, nextx = 1, 0
    prevy, nexty = 0, 1
    while b:
        quotient = a//b
        nextx, prevx = prevx - quotient * nextx, nextx
        nexty, prevy = prevy - quotient * nexty, nexty
        a, b = b, a % b
        # print(a,b)
    return prevx, prevy,a

x, y, g = xgcd(P, -Q)

a, b = divmod(x, Q)
x -= Q * a
y -= P * a

mx = Q - x
my = P - y

print(x + y + mx + my)
0