結果

問題 No.25 有限小数
ユーザー yuppe19 😺yuppe19 😺
提出日時 2016-01-04 00:00:49
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
RE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,920 bytes
コンパイル時間 94 ms
コンパイル使用メモリ 11,108 KB
実行使用メモリ 9,936 KB
最終ジャッジ日時 2023-08-09 22:57:25
合計ジャッジ時間 2,342 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/python3
from collections import defaultdict, deque
from fractions import gcd
from functools import reduce
from operator import mul
from random import randrange

def rabin_miller(n):
    if n < 2:
        return False
    if n != 2 and n % 2 == 0:
        return False
    s = n - 1
    while s % 2 == 0:
        s >>= 1
    for _ in range(10):
        a = randrange(n-1) + 1
        tmp = s
        mod = pow(a, tmp, n)
        while tmp != n-1 and mod != 1 and mod != n-1:
            mod = (mod * mod) % n
            tmp *= 2
        if mod != n-1 and tmp % 2 == 0:
            return False
    return True

def brent(n):
    if n % 2 == 0:
        return 2
    x = randrange(0, n)
    c = randrange(1, n)
    m = randrange(1, n)
    y, r, q = x, 1, 1
    g, ys = 0, 0
    while g <= 1:
        x = y
        for _ in range(r):
            y = (y*y+c)%n
        k = 0
        while k < r and g <= 1:
            ys = y
            for _ in range(min(m, r-k)):
                y = (y*y+c)%n
                q = (q*abs(x-y)) % n
            g, k = gcd(q,n), k+m
        r <<= 1
    if g == n:
        while g <= 1:
            ys = (ys*ys+c)%n
            g = gcd(abs(x-ys), n)
    return g

def factorize(n):
    que = deque()
    res = defaultdict(int)
    if n == 1:
        res[1] = 1
        return res
    que.append(n)
    while que:
        l = que.pop()
        if rabin_miller(l):
            res[l] += 1
            continue
        d = brent(l)
        que.append(d)
        if d != l:
            que.append(l // d)
    return res

###
n, m = int(input()), int(input())
x = factorize(n)
y = factorize(m)

yy = max(y[2], y[5])

x[2] += yy - y[2]
x[5] += yy - y[5]
y[2] = 0
y[5] = 0

xx = min(x[2], x[5])
x[2] -= xx
x[5] -= xx

for k in y.keys():
    if y[k] > x[k]:
        print(-1)
        exit(0)
    else:
        x[k] -= y[k]

res = reduce(mul, (k**v for k, v in x.items())) % 10
print(res)
0