結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー neterukunneterukun
提出日時 2021-03-05 13:04:50
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 483 ms / 9,973 ms
コード長 612 bytes
コンパイル時間 148 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 77,312 KB
最終ジャッジ日時 2024-04-28 09:42:41
合計ジャッジ時間 2,768 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
51,712 KB
testcase_01 AC 34 ms
51,840 KB
testcase_02 AC 34 ms
52,096 KB
testcase_03 AC 51 ms
51,840 KB
testcase_04 AC 315 ms
77,312 KB
testcase_05 AC 297 ms
76,800 KB
testcase_06 AC 160 ms
76,672 KB
testcase_07 AC 159 ms
76,544 KB
testcase_08 AC 157 ms
76,928 KB
testcase_09 AC 483 ms
76,800 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def miller_rabin(n):
    if n == 2:
        return True
    if n == 1 or n % 2 == 0:
        return False

    s = 0
    d = n - 1
    while d & 1 == 0:
        s += 1
        d >>= 1
    for a in (2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37):
        if n <= a:
            break
        x = pow(a, d, n)
        if x == 1:
            continue
        for _ in range(s):
            if x == n - 1:
                break
            x = x * x % n
        else:
            return False

    return True


n = int(input())
x = [int(input()) for i in range(n)]

for val in x:
    print(val, int(miller_rabin(val)))
0