結果

問題 No.25 有限小数
ユーザー yuppe19 😺yuppe19 😺
提出日時 2016-01-04 00:00:49
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
RE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,920 bytes
コンパイル時間 401 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 11,520 KB
最終ジャッジ日時 2024-11-15 21:14:42
合計ジャッジ時間 2,249 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other RE * 31
権限があれば一括ダウンロードができます

ソースコード

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