結果

問題 No.1339 循環小数
ユーザー H3PO4
提出日時 2021-01-15 22:15:12
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 133 ms / 2,000 ms
コード長 1,158 bytes
コンパイル時間 148 ms
コンパイル使用メモリ 82,188 KB
実行使用メモリ 66,108 KB
最終ジャッジ日時 2024-11-26 15:47:45
合計ジャッジ時間 3,694 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

# 参考:http://www2r.biglobe.ne.jp/~kosanhp/math/junkan.pdf

from math import gcd

lcm = lambda a, b: a * b // gcd(a, b)


def make_divisors(n):
    for i in range(1, int(n ** 0.5) + 1):
        if n % i == 0:
            yield i
            if i != n // i:
                yield n // i


def factorization(n):
    arr = dict()
    temp = n
    for i in range(2, int(-(-n ** 0.5 // 1)) + 1):
        if temp % i == 0:
            cnt = 0
            while temp % i == 0:
                cnt += 1
                temp //= i
            arr[i] = cnt

    if temp != 1:
        arr[temp] = 1

    if not arr and n != 1:
        arr[n] = 1

    return arr


def solve(N):
    while not N % 2:
        N //= 2
    while not N % 5:
        N //= 5
    if N == 1:
        return 1

    res = 1

    for p, idx in factorization(N).items():
        l = N
        for d in make_divisors(p - 1):
            if pow(10, d, p) == 1:
                l = min(l, d)

        e = (2 if p in (3, 487) else 1)

        if idx > e:
            l *= p ** (idx - e)

        res = lcm(res, l)
    return res


T = int(input())
for _ in range(T):
    print(solve(int(input())))
0