結果

問題 No.1936 Rational Approximation
ユーザー ygd.ygd.
提出日時 2022-05-13 22:13:13
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 75 ms / 2,000 ms
コード長 1,266 bytes
コンパイル時間 294 ms
コンパイル使用メモリ 87,372 KB
実行使用メモリ 71,688 KB
最終ジャッジ日時 2023-09-29 07:33:45
合計ジャッジ時間 2,230 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,436 KB
testcase_01 AC 71 ms
71,336 KB
testcase_02 AC 72 ms
71,392 KB
testcase_03 AC 73 ms
71,368 KB
testcase_04 AC 75 ms
71,552 KB
testcase_05 AC 73 ms
71,556 KB
testcase_06 AC 71 ms
71,372 KB
testcase_07 AC 72 ms
71,320 KB
testcase_08 AC 71 ms
71,612 KB
testcase_09 AC 73 ms
71,688 KB
testcase_10 AC 72 ms
71,240 KB
testcase_11 AC 72 ms
71,180 KB
testcase_12 AC 72 ms
71,540 KB
testcase_13 AC 71 ms
71,508 KB
testcase_14 AC 72 ms
71,472 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
#input = sys.stdin.readline
input = sys.stdin.buffer.readline #文字列はダメ
#sys.setrecursionlimit(1000000)
#import bisect
#import itertools
#import random
#from heapq import heapify, heappop, heappush
#from collections import defaultdict 
#from collections import deque
#import copy
#import math
#from functools import lru_cache
#@lru_cache(maxsize=None)
#MOD = pow(10,9) + 7
#MOD = 998244353
#dx = [1,0,-1,0]
#dy = [0,1,0,-1]
#dx8 = [1,1,0,-1,-1,-1,0,1]
#dy8 = [0,1,1,1,0,-1,-1,-1]

def calc(X):
    n = len(X)
    for i in reversed(range(n-1)):
        if i == n-2:
            a = X[i]
            b = X[i+1]
            c = 1
        else:
            a = X[i]
        nc = b
        nb = a*b + c
        b,c = nb,nc
    #print(b,c)
    return b,c

def main():
    P,Q = map(int,input().split())
    A = [0]
    while True:
        #print(P,Q)
        v = Q // P
        if P == 1:
            #print("last",A)
            if Q != 1:
                A.append(Q-1)
            A.append(1)
            break
        A.append(v)
        q = Q; p = P
        P = q - p*v
        Q = p
    #print(A)
    P0,Q0 = calc(A[:-1])
    P1,Q1 = calc(A[:-2])
    ans = P0 + Q0 + P1 + Q1
    print(ans)
                
if __name__ == '__main__':
    main()

0