結果

問題 No.1339 循環小数
ユーザー aaaaaaaaaa2230
提出日時 2021-01-16 10:15:38
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 451 ms / 2,000 ms
コード長 667 bytes
コンパイル時間 484 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 129,968 KB
最終ジャッジ日時 2024-11-27 10:10:16
合計ジャッジ時間 7,547 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

def baby_step_giant_step(a,b,mod):
    a %= mod
    b %= mod
    l = int(mod**0.5)+1
    h = 1 if mod != 0 else 0
    dic = {}
    for i in range(l):
        if h == b and i != 0:
            return i
        dic[h*b%mod] = i
        h *= a
        h %= mod
    g = h
    for i in range(l):
        if g in dic:
            res = (i+1)*l-dic[g]
            if pow(a,res,mod) == b:
                return res
            else:
                return -1
        g *= h
        g %= mod
    return -1

t = int(input())
for _ in range(t):
    n = int(input())
    while n%2 == 0:
        n //= 2
    while n%5 == 0:
        n //= 5
    print(baby_step_giant_step(10,1,n))
0