結果

問題 No.25 有限小数
ユーザー OKCH3COOHOKCH3COOH
提出日時 2019-10-25 15:55:13
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
MLE  
実行時間 -
コード長 1,527 bytes
コンパイル時間 79 ms
コンパイル使用メモリ 10,988 KB
実行使用メモリ 817,036 KB
最終ジャッジ日時 2023-09-27 07:39:26
合計ジャッジ時間 5,180 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 21 ms
8,944 KB
testcase_01 AC 19 ms
9,000 KB
testcase_02 AC 18 ms
8,900 KB
testcase_03 AC 18 ms
8,756 KB
testcase_04 AC 34 ms
14,208 KB
testcase_05 MLE -
testcase_06 AC 22 ms
9,960 KB
testcase_07 AC 241 ms
92,320 KB
testcase_08 AC 20 ms
9,512 KB
testcase_09 AC 33 ms
14,156 KB
testcase_10 MLE -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict

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

def primeCount(N):
    R = int(N**(0.5)) + 1  # 素数の範囲
    primes = {}  # 素数のリスト
    n = N
    for num in range(2, R):
        primes[num] = 0
        while n % num == 0:
            n //= num
            primes[num] += 1
    if n > 1 :
        primes[n] = 1

    ret = defaultdict(int)
    for key, val in primes.items():
        if val > 0:
            ret[key] = val
    return ret

def gcd(n, m):
    if m == 0:
        return n
    return gcd(m, n % m)

G = gcd(N, M)
N //= G
M //= G

# 10の約数(2, 5)以外を持つと無限小数
primes = primeCount(M)
divs = list(primes.keys())

if not divs:
    ans = ''
    for s in str(N)[:: -1]:
        if s != '0':
            print(s)
            exit()

if [2, 5] != divs and [5, 2] != divs and [2] != divs and [5] != divs:
    print(-1)
else:
    if primes[2] == primes[5]:
        print(str(N)[-1])
    elif primes[2] > primes[5]:
        primes[2] -= primes[5]
        while primes[2] > 0 and N % 2 == 0:
            N //= 2
            primes[2] -= 1
        if primes[2] == 0:
            print(str(N)[-1])
        else:
            print(5)
    else:
        primes[5] -= primes[2]
        while primes[5] > 0 and N % 5 == 0:
            N //= 5
            primes[5] -= 1
        if primes[5] == 0:
            print(str(N)[-1])
        else:
            ans = (N % 5) * 2
            primes[5] -= 1
            ans *= pow(2, primes[5], 10)
            print(str(ans)[-1])

0