結果

問題 No.1339 循環小数
ユーザー shotoyooshotoyoo
提出日時 2021-01-15 22:32:32
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 631 ms / 2,000 ms
コード長 1,486 bytes
コンパイル時間 311 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 153,728 KB
最終ジャッジ日時 2024-05-05 00:34:34
合計ジャッジ時間 8,736 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
52,480 KB
testcase_01 AC 47 ms
59,392 KB
testcase_02 AC 45 ms
60,032 KB
testcase_03 AC 43 ms
59,392 KB
testcase_04 AC 43 ms
58,752 KB
testcase_05 AC 44 ms
59,264 KB
testcase_06 AC 46 ms
59,136 KB
testcase_07 AC 43 ms
59,392 KB
testcase_08 AC 44 ms
59,136 KB
testcase_09 AC 44 ms
58,880 KB
testcase_10 AC 43 ms
58,880 KB
testcase_11 AC 51 ms
63,488 KB
testcase_12 AC 52 ms
63,872 KB
testcase_13 AC 51 ms
63,360 KB
testcase_14 AC 51 ms
64,640 KB
testcase_15 AC 50 ms
63,488 KB
testcase_16 AC 49 ms
63,104 KB
testcase_17 AC 50 ms
63,616 KB
testcase_18 AC 51 ms
63,360 KB
testcase_19 AC 50 ms
62,720 KB
testcase_20 AC 50 ms
63,488 KB
testcase_21 AC 373 ms
123,008 KB
testcase_22 AC 410 ms
135,680 KB
testcase_23 AC 406 ms
135,056 KB
testcase_24 AC 383 ms
127,616 KB
testcase_25 AC 405 ms
136,840 KB
testcase_26 AC 401 ms
132,992 KB
testcase_27 AC 414 ms
134,564 KB
testcase_28 AC 407 ms
139,264 KB
testcase_29 AC 350 ms
124,544 KB
testcase_30 AC 371 ms
126,464 KB
testcase_31 AC 572 ms
152,064 KB
testcase_32 AC 575 ms
153,728 KB
testcase_33 AC 398 ms
128,640 KB
testcase_34 AC 235 ms
109,696 KB
testcase_35 AC 631 ms
151,936 KB
testcase_36 AC 383 ms
123,008 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = lambda : sys.stdin.readline().rstrip()

sys.setrecursionlimit(2*10**5+10)
write = lambda x: sys.stdout.write(x+"\n")
_print = lambda *x: print(*x, file=sys.stderr)

def gcd2(a, b):
    """a*x + b*y = gcd(a,b)なるx,yも求める
    """
    l = []
    while b:
        l.append(divmod(a,b))
        a, b = b, a%b
    x, y = 1, 0
    for aa,bb in l[::-1]:
        x, y = y, x - aa*y
    return a, x, y

def modinv(x, M):
    """素数ではないM、Mと互いに素なxに対し
    x * y == 1 mod M なるyを求める
    """
    a,xx,yy = gcd2(x,M)
    return a,xx%M

### 離散対数問題
def dlog(x,y,M):
    """pow(x,i,M)==y なるiを返す
    存在しない場合Noneを返す
    """
    l = int(M**0.5)+1
    s = set()
    d = {}
    v = x%M
    for i in range(1,l+2):
        s.add(v)
        d[v] = i
        v *= x
        v %= M
    v = y
#     print(d)
    _,tmp = modinv(pow(x,l,M), M)
#     tmp = pow(pow(x,l,M), M-2, M)
    for i in range(l+1):
        if v in s:
            vv = x%M
            for j in range(1,l+2):
                if vv==v:
                    break
                vv *= x
                vv %= M
#             j = d[v]
            return i*l + j
        v *= tmp
        v %= M
    return None

t = int(input())
from math import gcd
for _ in range(t):
    n = int(input())
    while n%2==0:
        n //= 2
    while n%5==0:
        n //= 5
    if n==1:
        ans = 1
    else:
        ans = dlog(10,1,n)
    print(ans)
0