結果

問題 No.1936 Rational Approximation
ユーザー ygd.ygd.
提出日時 2022-05-13 22:13:13
言語 PyPy3
(7.3.8)
結果
AC  
実行時間 85 ms / 2,000 ms
コード長 1,266 bytes
コンパイル時間 271 ms
使用メモリ 75,756 KB
最終ジャッジ日時 2023-02-21 14:53:06
合計ジャッジ時間 2,518 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 81 ms
75,744 KB
testcase_01 AC 83 ms
75,728 KB
testcase_02 AC 81 ms
75,752 KB
testcase_03 AC 82 ms
75,720 KB
testcase_04 AC 81 ms
75,712 KB
testcase_05 AC 82 ms
75,412 KB
testcase_06 AC 81 ms
75,624 KB
testcase_07 AC 81 ms
75,516 KB
testcase_08 AC 81 ms
75,672 KB
testcase_09 AC 81 ms
75,712 KB
testcase_10 AC 81 ms
75,756 KB
testcase_11 AC 85 ms
75,752 KB
testcase_12 AC 81 ms
75,504 KB
testcase_13 AC 83 ms
75,448 KB
testcase_14 AC 82 ms
75,440 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