結果

問題 No.1339 循環小数
ユーザー H3PO4H3PO4
提出日時 2021-01-15 22:15:12
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 177 ms / 2,000 ms
コード長 1,158 bytes
コンパイル時間 434 ms
コンパイル使用メモリ 87,232 KB
実行使用メモリ 76,864 KB
最終ジャッジ日時 2023-08-17 17:29:42
合計ジャッジ時間 5,709 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,940 KB
testcase_01 AC 83 ms
75,668 KB
testcase_02 AC 84 ms
75,776 KB
testcase_03 AC 79 ms
75,828 KB
testcase_04 AC 79 ms
75,768 KB
testcase_05 AC 79 ms
76,148 KB
testcase_06 AC 80 ms
76,064 KB
testcase_07 AC 77 ms
75,780 KB
testcase_08 AC 79 ms
75,808 KB
testcase_09 AC 80 ms
75,532 KB
testcase_10 AC 80 ms
75,724 KB
testcase_11 AC 86 ms
76,356 KB
testcase_12 AC 85 ms
76,416 KB
testcase_13 AC 87 ms
76,592 KB
testcase_14 AC 85 ms
76,196 KB
testcase_15 AC 86 ms
76,624 KB
testcase_16 AC 83 ms
76,380 KB
testcase_17 AC 84 ms
76,468 KB
testcase_18 AC 87 ms
76,736 KB
testcase_19 AC 85 ms
76,008 KB
testcase_20 AC 86 ms
76,336 KB
testcase_21 AC 126 ms
76,600 KB
testcase_22 AC 131 ms
76,800 KB
testcase_23 AC 130 ms
76,688 KB
testcase_24 AC 121 ms
76,684 KB
testcase_25 AC 134 ms
76,764 KB
testcase_26 AC 127 ms
76,400 KB
testcase_27 AC 132 ms
76,780 KB
testcase_28 AC 131 ms
76,620 KB
testcase_29 AC 125 ms
76,796 KB
testcase_30 AC 125 ms
76,492 KB
testcase_31 AC 136 ms
76,664 KB
testcase_32 AC 140 ms
76,848 KB
testcase_33 AC 130 ms
76,708 KB
testcase_34 AC 96 ms
75,888 KB
testcase_35 AC 177 ms
76,600 KB
testcase_36 AC 128 ms
76,864 KB
権限があれば一括ダウンロードができます

ソースコード

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