結果

問題 No.1936 Rational Approximation
ユーザー tktk_snsntktk_snsn
提出日時 2022-05-13 22:16:11
言語 Python3
(3.10.1 + numpy 1.22.3 + scipy 1.8.0)
結果
AC  
実行時間 16 ms / 2,000 ms
コード長 507 bytes
コンパイル時間 58 ms
使用メモリ 7,800 KB
最終ジャッジ日時 2023-02-21 15:00:49
合計ジャッジ時間 1,474 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 16 ms
7,748 KB
testcase_01 AC 15 ms
7,640 KB
testcase_02 AC 14 ms
7,696 KB
testcase_03 AC 14 ms
7,536 KB
testcase_04 AC 14 ms
7,704 KB
testcase_05 AC 14 ms
7,756 KB
testcase_06 AC 15 ms
7,644 KB
testcase_07 AC 15 ms
7,600 KB
testcase_08 AC 15 ms
7,524 KB
testcase_09 AC 15 ms
7,724 KB
testcase_10 AC 15 ms
7,576 KB
testcase_11 AC 15 ms
7,648 KB
testcase_12 AC 15 ms
7,700 KB
testcase_13 AC 15 ms
7,800 KB
testcase_14 AC 15 ms
7,636 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

"""faray数列?"""

def extgcd(a, b):
    """
    拡張Euclidの互除法
    INPUT:
        a, b
    OUTPUT:
        d: gcd(a, b)
        (x, y): ax + by = d の解
    """
    if b == 0:
        return a, (1, 0)
    d, (y, x) = extgcd(b, a % b)
    y -= a // b * x
    return d, (x, y)


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

d, (x, y) = extgcd(P, -Q)
if d == -1:
    x = -x
    y = -y
x %= Q
y %= P

dd, (xx, yy) = extgcd(Q, -P)
if dd == -1:
    xx = -xx
    yy = -yy
xx %= P
yy %= Q

print(x + y + xx + yy)
0