結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー 👑 MizarMizar
提出日時 2022-08-26 21:28:52
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 386 ms / 9,973 ms
コード長 724 bytes
コンパイル時間 275 ms
コンパイル使用メモリ 86,540 KB
実行使用メモリ 78,400 KB
最終ジャッジ日時 2023-08-10 16:39:27
合計ジャッジ時間 2,905 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 69 ms
71,116 KB
testcase_01 AC 68 ms
71,136 KB
testcase_02 AC 68 ms
71,024 KB
testcase_03 AC 65 ms
71,188 KB
testcase_04 AC 298 ms
78,400 KB
testcase_05 AC 289 ms
77,800 KB
testcase_06 AC 205 ms
77,604 KB
testcase_07 AC 205 ms
78,108 KB
testcase_08 AC 204 ms
77,864 KB
testcase_09 AC 386 ms
77,556 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def miller_rabin(n: int):
    if n == 2:
        return True
    if n < 2 or (n & 1) == 0:
        return False
    n1 = n - 1
    d = n1
    s = 0
    while (d & 1) == 0:
        d //= 2
        s += 1
    for a in [2,325,9375,28178,450775,9780504,1795265022]:
        if a % n == 0:
            continue
        t = pow(a, d, n) # = pow(a, d) % n
        if t == 1 or t == n1:
            continue
        for _ in range(s - 1):
            t = pow(t, 2, n) # = pow(t, 2) % n
            if t == n1:
                break
        else: # breakでループを抜けなかった時
            return False
    return True

n = int(input())

for _ in range(n):
    x = int(input())
    print(x, 1 if miller_rabin(x) else 0)
0