結果

問題 No.25 有限小数
ユーザー OKCH3COOH
提出日時 2019-10-25 15:55:13
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
MLE  
実行時間 -
コード長 1,527 bytes
コンパイル時間 126 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 817,948 KB
最終ジャッジ日時 2024-07-20 01:47:39
合計ジャッジ時間 6,352 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 9 MLE * 2 -- * 20
権限があれば一括ダウンロードができます

ソースコード

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