結果

問題 No.1339 循環小数
ユーザー mkawa2mkawa2
提出日時 2021-01-17 22:13:05
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 877 ms / 2,000 ms
コード長 1,787 bytes
コンパイル時間 98 ms
コンパイル使用メモリ 10,760 KB
実行使用メモリ 13,560 KB
最終ジャッジ日時 2023-08-20 04:00:50
合計ジャッジ時間 10,922 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
8,204 KB
testcase_01 AC 19 ms
8,136 KB
testcase_02 AC 16 ms
8,248 KB
testcase_03 AC 17 ms
8,060 KB
testcase_04 AC 17 ms
8,124 KB
testcase_05 AC 17 ms
8,116 KB
testcase_06 AC 18 ms
8,140 KB
testcase_07 AC 18 ms
8,236 KB
testcase_08 AC 17 ms
8,056 KB
testcase_09 AC 18 ms
8,100 KB
testcase_10 AC 17 ms
8,200 KB
testcase_11 AC 20 ms
8,052 KB
testcase_12 AC 20 ms
8,056 KB
testcase_13 AC 19 ms
8,204 KB
testcase_14 AC 20 ms
8,112 KB
testcase_15 AC 20 ms
8,204 KB
testcase_16 AC 20 ms
8,104 KB
testcase_17 AC 20 ms
8,048 KB
testcase_18 AC 19 ms
8,092 KB
testcase_19 AC 19 ms
8,136 KB
testcase_20 AC 20 ms
8,092 KB
testcase_21 AC 422 ms
12,840 KB
testcase_22 AC 474 ms
12,964 KB
testcase_23 AC 456 ms
12,824 KB
testcase_24 AC 426 ms
12,904 KB
testcase_25 AC 470 ms
12,920 KB
testcase_26 AC 470 ms
12,908 KB
testcase_27 AC 455 ms
12,752 KB
testcase_28 AC 477 ms
13,088 KB
testcase_29 AC 387 ms
12,848 KB
testcase_30 AC 434 ms
12,940 KB
testcase_31 AC 703 ms
13,560 KB
testcase_32 AC 726 ms
12,844 KB
testcase_33 AC 463 ms
12,960 KB
testcase_34 AC 310 ms
12,960 KB
testcase_35 AC 877 ms
12,852 KB
testcase_36 AC 444 ms
12,884 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

sys.setrecursionlimit(10**6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.buffer.readline())
def MI(): return map(int, sys.stdin.buffer.readline().split())
def MI1(): return map(int1, sys.stdin.buffer.readline().split())
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def LI1(): return list(map(int1, sys.stdin.buffer.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def BI(): return sys.stdin.buffer.readline().rstrip()
def SI(): return sys.stdin.buffer.readline().rstrip().decode()
dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
inf = 10**19
# md = 998244353
md = 10**9 + 7

def extgcd(x, y):
    a, b = 1, 0
    while y:
        a, b = b, (a - x // y * b)
        x, y = y, x % y
    return a

def BabyGiant(a, b, md):
    a, b = a % md, b % md

    if md == 1: return 0
    if a == b == 0: return 1
    if b == 1: return 0

    def gcd(a, b):
        while b: a, b = b, a % b
        return a

    c = 0
    while 1:
        g = gcd(a, md)
        if b == 1 or md == 1: return c
        if b % g: return -1
        if g == 1: break
        b //= g
        md //= g
        b = b * extgcd(a // g, md) % md
        c += 1
    a %= md

    # baby step
    m = int(md**.5) + 1
    baby = {}
    x = 1
    for i in range(m):
        if x == b: return i + c
        baby[x] = i
        x = x * a % md
    if b in baby:
        return baby[b] + c

    # giant step
    inv = extgcd(x, md)
    for i in range(1, m + 1):
        b = b * inv % md
        if b in baby:
            return i * m + baby[b] + c
    return -1

for _ in range(II()):
    n = II()
    while n & 1 == 0: n >>= 1
    while n % 5 == 0: n //= 5
    ans = BabyGiant(10, extgcd(10, n), n) + 1
    print(ans)
0