結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
52,224 KB
testcase_01 AC 33 ms
51,968 KB
testcase_02 AC 34 ms
52,608 KB
testcase_03 AC 35 ms
52,224 KB
testcase_04 AC 327 ms
77,312 KB
testcase_05 AC 322 ms
76,672 KB
testcase_06 AC 190 ms
76,928 KB
testcase_07 AC 185 ms
76,672 KB
testcase_08 AC 188 ms
76,928 KB
testcase_09 AC 509 ms
77,056 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]
    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, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
setP = set(P)
def isPrime(n):
    if n in setP:
        return 1
    if n < 100:
        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