結果

問題 No.751 Frac #2
ユーザー ThetaTheta
提出日時 2022-10-19 14:52:44
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 21 ms / 1,000 ms
コード長 1,221 bytes
コンパイル時間 259 ms
コンパイル使用メモリ 10,872 KB
実行使用メモリ 8,812 KB
最終ジャッジ日時 2023-09-12 04:33:31
合計ジャッジ時間 2,751 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 20 ms
8,600 KB
testcase_01 AC 20 ms
8,784 KB
testcase_02 AC 20 ms
8,796 KB
testcase_03 AC 20 ms
8,792 KB
testcase_04 AC 20 ms
8,632 KB
testcase_05 AC 20 ms
8,776 KB
testcase_06 AC 20 ms
8,764 KB
testcase_07 AC 20 ms
8,796 KB
testcase_08 AC 20 ms
8,628 KB
testcase_09 AC 21 ms
8,668 KB
testcase_10 AC 20 ms
8,748 KB
testcase_11 AC 20 ms
8,636 KB
testcase_12 AC 20 ms
8,784 KB
testcase_13 AC 19 ms
8,800 KB
testcase_14 AC 20 ms
8,636 KB
testcase_15 AC 20 ms
8,772 KB
testcase_16 AC 20 ms
8,780 KB
testcase_17 AC 20 ms
8,672 KB
testcase_18 AC 20 ms
8,784 KB
testcase_19 AC 20 ms
8,588 KB
testcase_20 AC 21 ms
8,740 KB
testcase_21 AC 20 ms
8,620 KB
testcase_22 AC 20 ms
8,632 KB
testcase_23 AC 20 ms
8,628 KB
testcase_24 AC 20 ms
8,584 KB
testcase_25 AC 20 ms
8,780 KB
testcase_26 AC 20 ms
8,676 KB
testcase_27 AC 20 ms
8,780 KB
testcase_28 AC 20 ms
8,812 KB
testcase_29 AC 20 ms
8,604 KB
testcase_30 AC 20 ms
8,636 KB
testcase_31 AC 21 ms
8,764 KB
testcase_32 AC 21 ms
8,748 KB
testcase_33 AC 20 ms
8,772 KB
testcase_34 AC 20 ms
8,596 KB
testcase_35 AC 20 ms
8,720 KB
testcase_36 AC 20 ms
8,792 KB
testcase_37 AC 20 ms
8,680 KB
権限があれば一括ダウンロードができます

ソースコード

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