結果

問題 No.1339 循環小数
ユーザー shotoyooshotoyoo
提出日時 2021-01-15 22:32:32
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 695 ms / 2,000 ms
コード長 1,486 bytes
コンパイル時間 275 ms
コンパイル使用メモリ 86,916 KB
実行使用メモリ 154,156 KB
最終ジャッジ日時 2023-08-17 17:52:48
合計ジャッジ時間 11,095 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
71,228 KB
testcase_01 AC 96 ms
75,824 KB
testcase_02 AC 77 ms
76,576 KB
testcase_03 AC 80 ms
75,872 KB
testcase_04 AC 78 ms
75,544 KB
testcase_05 AC 79 ms
75,712 KB
testcase_06 AC 80 ms
75,832 KB
testcase_07 AC 77 ms
75,880 KB
testcase_08 AC 77 ms
75,832 KB
testcase_09 AC 76 ms
75,976 KB
testcase_10 AC 77 ms
75,880 KB
testcase_11 AC 82 ms
76,164 KB
testcase_12 AC 84 ms
75,880 KB
testcase_13 AC 82 ms
76,272 KB
testcase_14 AC 82 ms
76,584 KB
testcase_15 AC 81 ms
76,060 KB
testcase_16 AC 81 ms
76,072 KB
testcase_17 AC 81 ms
76,424 KB
testcase_18 AC 80 ms
76,092 KB
testcase_19 AC 79 ms
76,272 KB
testcase_20 AC 79 ms
76,144 KB
testcase_21 AC 419 ms
128,200 KB
testcase_22 AC 468 ms
143,100 KB
testcase_23 AC 452 ms
134,536 KB
testcase_24 AC 430 ms
135,744 KB
testcase_25 AC 441 ms
127,568 KB
testcase_26 AC 448 ms
133,640 KB
testcase_27 AC 461 ms
136,380 KB
testcase_28 AC 467 ms
138,580 KB
testcase_29 AC 408 ms
127,936 KB
testcase_30 AC 426 ms
125,396 KB
testcase_31 AC 643 ms
153,776 KB
testcase_32 AC 640 ms
153,768 KB
testcase_33 AC 453 ms
130,896 KB
testcase_34 AC 288 ms
114,612 KB
testcase_35 AC 695 ms
154,156 KB
testcase_36 AC 433 ms
126,260 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