結果

問題 No.1936 Rational Approximation
ユーザー 👑 tatyamtatyam
提出日時 2022-04-07 16:44:22
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 273 ms / 2,000 ms
コード長 573 bytes
コンパイル時間 2,250 ms
コンパイル使用メモリ 86,920 KB
実行使用メモリ 90,396 KB
最終ジャッジ日時 2023-08-18 18:06:06
合計ジャッジ時間 6,384 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 233 ms
88,936 KB
testcase_01 AC 233 ms
89,092 KB
testcase_02 AC 232 ms
88,920 KB
testcase_03 AC 246 ms
89,112 KB
testcase_04 AC 237 ms
89,172 KB
testcase_05 AC 239 ms
89,180 KB
testcase_06 AC 239 ms
89,224 KB
testcase_07 AC 273 ms
90,396 KB
testcase_08 AC 240 ms
89,348 KB
testcase_09 AC 240 ms
89,340 KB
testcase_10 AC 238 ms
89,168 KB
testcase_11 AC 240 ms
89,184 KB
testcase_12 AC 238 ms
89,052 KB
testcase_13 AC 238 ms
89,124 KB
testcase_14 AC 231 ms
89,000 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from fractions import *
P, Q = map(int, input().split())
x = P / Q
X = Fraction(P, Q)

ok = Fraction(0)
ng = X
while True:
    mid = ((ok + ng) / Fraction(2)).limit_denominator(Q - 1)
    if mid == ok or mid == ng:
        break
    if mid < X:
        ok = mid
    else:
        ng = mid

L = ok

ok = Fraction(1)
ng = X
while True:
    mid = ((ok + ng) / Fraction(2)).limit_denominator(Q - 1)
    if mid == ok or mid == ng:
        break
    if mid > X:
        ok = mid
    else:
        ng = mid

R = ok

print(L.numerator + L.denominator + R.numerator + R.denominator)
0