結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
51,712 KB
testcase_01 AC 38 ms
51,712 KB
testcase_02 AC 38 ms
51,840 KB
testcase_03 AC 39 ms
52,096 KB
testcase_04 AC 332 ms
76,800 KB
testcase_05 AC 337 ms
77,184 KB
testcase_06 AC 187 ms
76,800 KB
testcase_07 AC 189 ms
76,800 KB
testcase_08 AC 187 ms
76,928 KB
testcase_09 AC 519 ms
76,544 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def isPrimeMR(n):
    d = n - 1
    d = d // (d & -d)
    L = [2, 7, 61] if n < 1<<32 else [2, 3, 5, 7, 11, 13, 17] if n < 1<<48 else [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]
    if n in L: return 1
    for a in L:
        t = d
        y = pow(a, t, n)
        if y == 1: continue
        while y != n - 1:
            y = y * y % n
            if y == 1 or t == n - 1: return 0
            t <<= 1
    return 1

P = [2, 3, 5, 7, 11, 13, 17, 19]
setP = set(P)
def isPrime(n):
    if n in setP:
        return 1
    if n < 20:
        return 0
    for p in P:
        if n % p == 0:
            return 0
    return isPrimeMR(n)

N = int(input())
for _ in range(N):
    n = int(input())
    print(n, isPrime(n))
0