結果

問題 No.1936 Rational Approximation
ユーザー 粟野翔太粟野翔太
提出日時 2022-05-13 22:55:06
言語 PyPy3
(7.3.8)
結果
AC  
実行時間 87 ms / 2,000 ms
コード長 503 bytes
コンパイル時間 396 ms
使用メモリ 75,860 KB
最終ジャッジ日時 2023-02-21 16:27:13
合計ジャッジ時間 2,847 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 83 ms
75,860 KB
testcase_01 AC 84 ms
75,472 KB
testcase_02 AC 83 ms
75,468 KB
testcase_03 AC 85 ms
75,644 KB
testcase_04 AC 85 ms
75,752 KB
testcase_05 AC 86 ms
75,524 KB
testcase_06 AC 87 ms
75,796 KB
testcase_07 AC 85 ms
75,792 KB
testcase_08 AC 86 ms
75,692 KB
testcase_09 AC 83 ms
75,472 KB
testcase_10 AC 84 ms
75,796 KB
testcase_11 AC 85 ms
75,548 KB
testcase_12 AC 84 ms
75,748 KB
testcase_13 AC 83 ms
75,664 KB
testcase_14 AC 83 ms
75,776 KB
権限があれば一括ダウンロードができます

ソースコード

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