結果

問題 No.8030 ミラー・ラビン素数判定法のテスト
ユーザー iorion
提出日時 2022-02-25 06:42:51
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 2,662 ms / 9,973 ms
コード長 728 bytes
コンパイル時間 431 ms
コンパイル使用メモリ 12,416 KB
実行使用メモリ 10,752 KB
最終ジャッジ日時 2024-11-16 23:46:25
合計ジャッジ時間 7,865 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 10
権限があれば一括ダウンロードができます

ソースコード

diff #

def miller_rabin(n: int) -> bool:
    if n <= 2:
        return n == 2
    if n % 2 == 0:
        return False
    
    s = 0
    t = n - 1
    while t % 2 == 0:
        s += 1
        t //= 2
    
    def witness_composite(a: int) -> bool:
        x = pow(a, t, n)
        if x == 1 or x == n - 1:
            return False
        for _ in range(s - 1):
            x = pow(x, 2, n)
            if x == n - 1:
                return False
        return True

    for a in [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]:
        if a >= n:
            break
        if witness_composite(a):
            return False
    
    return True

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