結果

問題 No.25 有限小数
ユーザー konchanksukonchanksu
提出日時 2020-05-01 20:25:02
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 17 ms / 5,000 ms
コード長 915 bytes
コンパイル時間 105 ms
コンパイル使用メモリ 10,932 KB
実行使用メモリ 7,932 KB
最終ジャッジ日時 2023-08-26 04:34:04
合計ジャッジ時間 1,935 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
7,792 KB
testcase_01 AC 17 ms
7,748 KB
testcase_02 AC 15 ms
7,776 KB
testcase_03 AC 15 ms
7,924 KB
testcase_04 AC 16 ms
7,800 KB
testcase_05 AC 14 ms
7,904 KB
testcase_06 AC 15 ms
7,772 KB
testcase_07 AC 14 ms
7,788 KB
testcase_08 AC 14 ms
7,928 KB
testcase_09 AC 14 ms
7,824 KB
testcase_10 AC 14 ms
7,748 KB
testcase_11 AC 13 ms
7,864 KB
testcase_12 AC 14 ms
7,836 KB
testcase_13 AC 15 ms
7,832 KB
testcase_14 AC 15 ms
7,872 KB
testcase_15 AC 14 ms
7,800 KB
testcase_16 AC 14 ms
7,800 KB
testcase_17 AC 14 ms
7,932 KB
testcase_18 AC 14 ms
7,896 KB
testcase_19 AC 14 ms
7,828 KB
testcase_20 AC 14 ms
7,828 KB
testcase_21 AC 14 ms
7,748 KB
testcase_22 AC 14 ms
7,908 KB
testcase_23 AC 15 ms
7,872 KB
testcase_24 AC 14 ms
7,860 KB
testcase_25 AC 14 ms
7,872 KB
testcase_26 AC 15 ms
7,888 KB
testcase_27 AC 15 ms
7,908 KB
testcase_28 AC 14 ms
7,856 KB
testcase_29 AC 14 ms
7,800 KB
testcase_30 AC 14 ms
7,824 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N, M = int(input()), int(input())

def to_and_five(num, lists={2:0, 5:0}):
    if num == 1:
        return lists
    if num % 2 == 0:
        lists[2] += 1
        return to_and_five(num // 2, lists)
    if num % 5 == 0:
        lists[5] += 1
        return to_and_five(num // 5, lists)
    return False

def prime(n, m):
    if n < m:
        n, m = m, n
    if not (tmp := n % m):
        return m
    else:
        return prime(m, tmp)
    
if N % M == 0:
    ans = N // M
    while not ans % 10:
        ans //= 10
    ans %= 10
    
else:
    N, M = N // (tmp := prime(N, M)), M // tmp
    if (tmp := to_and_five(M)):
        if N >= 10:
            N %= 10
        if tmp[2] > tmp[5]:
            for i in range(tmp[2] - tmp[5]):
                N *= 5
        elif tmp[2] < tmp[5]:
            for i in range(tmp[5] - tmp[2]):
                N *= 2
        ans = N % 10
    else:
        ans = -1
print(ans)
0