結果

問題 No.1936 Rational Approximation
ユーザー tatyamtatyam
提出日時 2022-04-07 15:57:31
言語 PyPy3
(7.3.8)
結果
AC  
実行時間 302 ms / 2,000 ms
コード長 501 bytes
コンパイル時間 276 ms
使用メモリ 100,140 KB
最終ジャッジ日時 2023-01-04 20:56:47
合計ジャッジ時間 6,549 ms
ジャッジサーバーID
(参考情報)
judge10 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 286 ms
97,776 KB
testcase_01 AC 294 ms
99,668 KB
testcase_02 AC 278 ms
97,476 KB
testcase_03 AC 293 ms
99,848 KB
testcase_04 AC 291 ms
100,056 KB
testcase_05 AC 293 ms
99,740 KB
testcase_06 AC 289 ms
99,772 KB
testcase_07 AC 302 ms
98,944 KB
testcase_08 AC 289 ms
99,928 KB
testcase_09 AC 291 ms
99,772 KB
testcase_10 AC 299 ms
100,140 KB
testcase_11 AC 294 ms
100,128 KB
testcase_12 AC 298 ms
99,864 KB
testcase_13 AC 296 ms
99,812 KB
testcase_14 AC 284 ms
97,692 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

ok = Fraction(0)
ng = X
for i in range(200):
    mid = ((ok + ng) / Fraction(2)).limit_denominator(Q - 1)
    if mid < X:
        ok = mid
    else:
        ng = mid

L = ok

ok = Fraction(1)
ng = X
for i in range(200):
    mid = ((ok + ng) / Fraction(2)).limit_denominator(Q - 1)
    if mid > X:
        ok = mid
    else:
        ng = mid

R = ok

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