結果

問題 No.1936 Rational Approximation
コンテスト
ユーザー sotanishy
提出日時 2022-05-13 23:18:07
言語 PyPy3
(7.3.23 + ACL)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 56 ms / 2,000 ms
+ 318µs
コード長 587 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 220 ms
コンパイル使用メモリ 95,976 KB
実行使用メモリ 79,136 KB
最終ジャッジ日時 2026-08-22 21:20:41
合計ジャッジ時間 2,564 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 14
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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