結果

問題 No.1339 循環小数
ユーザー mkawa2
提出日時 2021-01-17 22:13:05
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 1,147 ms / 2,000 ms
コード長 1,787 bytes
コンパイル時間 254 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 15,324 KB
最終ジャッジ日時 2024-11-30 04:04:17
合計ジャッジ時間 12,796 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

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