結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー masa_aamasa_aa
提出日時 2020-09-21 01:48:56
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,046 ms / 9,973 ms
コード長 697 bytes
コンパイル時間 171 ms
コンパイル使用メモリ 82,816 KB
実行使用メモリ 77,056 KB
最終ジャッジ日時 2024-04-28 09:37:59
合計ジャッジ時間 4,135 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
52,224 KB
testcase_01 AC 41 ms
51,840 KB
testcase_02 AC 41 ms
51,840 KB
testcase_03 AC 41 ms
51,840 KB
testcase_04 AC 637 ms
77,056 KB
testcase_05 AC 612 ms
76,928 KB
testcase_06 AC 278 ms
76,288 KB
testcase_07 AC 274 ms
76,416 KB
testcase_08 AC 272 ms
76,672 KB
testcase_09 AC 1,046 ms
76,672 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def Miller(N):
    v = [2, 7, 61] if N < 4_759_123_141 else [2, 3, 5, 7, 11, 13, 17] if N < 341_550_071_728_321 else [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]
    if N < 2:
        return False
    if N in v:
        return True
    d = N - 1
    s = 0
    while d % 2 == 0:
        d //= 2
        s += 1

    for a in v:
        if pow(a, d, N) != 1:
            ok = True
            for r in range(s):
                if pow(a, d * 1 << r, N) == N - 1:
                    ok = 0
                    break
            if ok:
                return False
    return True


import sys
input = sys.stdin.readline
for _ in range(int(input())):
    n = int(input())
    print(n, int(Miller(n)))
0