結果

問題 No.751 Frac #2
ユーザー Theta
提出日時 2022-10-19 14:52:44
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 39 ms / 1,000 ms
コード長 1,221 bytes
コンパイル時間 113 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 10,752 KB
最終ジャッジ日時 2024-06-29 17:36:27
合計ジャッジ時間 2,351 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

from functools import reduce
from operator import mul


def positive_gcd(*numbers: int) -> int:
    numbers = list(map(abs, numbers))
    if len(numbers) == 1:
        return numbers[0]
    if len(numbers) == 2:
        a, b = numbers
        if a < b:
            a, b = b, a

        while True:

            if a % b == 0:
                return b
            a, b = b, a % b

    first_gcd = positive_gcd(*numbers[:2])
    return positive_gcd(first_gcd, *numbers[2:])


def main():
    n1 = int(input())
    A = list(map(int, input().split()))
    n2 = int(input())
    B = list(map(int, input().split()))

    A_child = A[0]

    if n1 == 1:
        A_parent = 1
    else:
        A_parent = reduce(mul, A[1:])

    B_child = 1
    B_parent = 1
    for idx, b_elm in enumerate(B):
        if idx % 2 == 0:
            B_child *= b_elm
        else:
            B_parent *= b_elm

    AB_child = A_child * B_parent
    AB_parent = A_parent * B_child

    gcd = positive_gcd(AB_child, AB_parent)
    AB_child //= gcd
    AB_parent //= gcd

    if AB_child * AB_parent < 0:
        print(-1 * abs(AB_child), abs(AB_parent))
    else:
        print(abs(AB_child), abs(AB_parent))


if __name__ == "__main__":
    main()
0