結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー iorioniorion
提出日時 2022-02-25 06:44:45
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 557 ms / 9,973 ms
コード長 728 bytes
コンパイル時間 209 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 77,440 KB
最終ジャッジ日時 2024-04-28 09:49:36
合計ジャッジ時間 2,828 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
51,840 KB
testcase_01 AC 37 ms
51,968 KB
testcase_02 AC 37 ms
52,480 KB
testcase_03 AC 35 ms
52,096 KB
testcase_04 AC 355 ms
77,184 KB
testcase_05 AC 342 ms
77,440 KB
testcase_06 AC 195 ms
76,672 KB
testcase_07 AC 220 ms
77,440 KB
testcase_08 AC 207 ms
76,672 KB
testcase_09 AC 557 ms
77,056 KB
権限があれば一括ダウンロードができます

ソースコード

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