結果

問題 No.1339 循環小数
ユーザー 草苺奶昔草苺奶昔
提出日時 2023-03-13 17:22:33
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 489 ms / 2,000 ms
コード長 1,029 bytes
コンパイル時間 893 ms
コンパイル使用メモリ 81,544 KB
実行使用メモリ 129,308 KB
最終ジャッジ日時 2023-10-18 11:11:28
合計ジャッジ時間 9,412 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
53,708 KB
testcase_01 AC 45 ms
59,660 KB
testcase_02 AC 45 ms
59,664 KB
testcase_03 AC 46 ms
59,664 KB
testcase_04 AC 45 ms
59,664 KB
testcase_05 AC 45 ms
59,664 KB
testcase_06 AC 45 ms
59,664 KB
testcase_07 AC 46 ms
60,060 KB
testcase_08 AC 45 ms
59,680 KB
testcase_09 AC 45 ms
59,680 KB
testcase_10 AC 45 ms
59,680 KB
testcase_11 AC 49 ms
61,820 KB
testcase_12 AC 48 ms
61,820 KB
testcase_13 AC 49 ms
61,820 KB
testcase_14 AC 48 ms
61,820 KB
testcase_15 AC 48 ms
61,820 KB
testcase_16 AC 48 ms
61,820 KB
testcase_17 AC 49 ms
61,820 KB
testcase_18 AC 48 ms
61,820 KB
testcase_19 AC 49 ms
61,820 KB
testcase_20 AC 48 ms
61,820 KB
testcase_21 AC 295 ms
109,940 KB
testcase_22 AC 327 ms
115,652 KB
testcase_23 AC 316 ms
111,872 KB
testcase_24 AC 295 ms
112,536 KB
testcase_25 AC 318 ms
112,488 KB
testcase_26 AC 316 ms
109,712 KB
testcase_27 AC 315 ms
112,384 KB
testcase_28 AC 326 ms
113,868 KB
testcase_29 AC 278 ms
111,184 KB
testcase_30 AC 297 ms
109,240 KB
testcase_31 AC 451 ms
129,024 KB
testcase_32 AC 458 ms
129,308 KB
testcase_33 AC 317 ms
112,596 KB
testcase_34 AC 207 ms
98,816 KB
testcase_35 AC 489 ms
129,076 KB
testcase_36 AC 310 ms
110,780 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from math import ceil, sqrt


def bsgs(base: int, target: int, p: int) -> int:
    """Baby-step Giant-step

    在base和p互质的情况下,求解 base^x ≡ target (mod p) 的最小解x,
    若不存在解则返回-1

    时间复杂度: O(sqrt(p)))

    https://dianhsu.com/2022/08/27/template-math/#bsgs
    """
    mp = dict()
    t = ceil(sqrt(p))
    target %= p
    val = 1
    for i in range(t):
        tv = target * val % p
        mp[tv] = i
        val = val * base % p

    base, val = val, 1
    if base == 0:
        return 1 if target == 0 else -1

    for i in range(t + 1):
        tv = mp.get(val, -1)
        if tv != -1 and i * t - tv > 0:  # !注意这里取等号表示允许最小解为0
            return i * t - tv
        val = val * base % p

    return -1


def solve(n: int) -> int:
    while n % 2 == 0:
        n //= 2
    while n % 5 == 0:
        n //= 5
    return bsgs(10, 1, n)


if __name__ == "__main__":
    T = int(input())
    for _ in range(T):
        print(solve(int(input())))
0