結果

問題 No.8030 ミラー・ラビン素数判定法のテスト
ユーザー masa_aa
提出日時 2021-03-11 16:54:01
言語 PyPy3
(8.0.0 + ACL)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 127 ms / 9,973 ms
+ 308µs
コード長 956 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 84 ms
コンパイル使用メモリ 64,668 KB
実行使用メモリ 61,188 KB
最終ジャッジ日時 2026-10-23 08:31:27
合計ジャッジ時間 2,353 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge5_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 11
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

def miller_rabin(n):
    """素数判定 log(n)"""
    if n < 2:
        return False

    base = [2, 7, 61] if n < 4_759_123_141 else \
           [2, 3, 5, 7, 11, 13, 17] if n < 341_550_071_728_321 else \
           [2, 3, 5, 7, 11, 13, 17, 19, 23] if n < 3_825_123_056_546_413_051 else \
           [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]

    if n in base:
        return True

    if n % 2 == 0:
        return False

    d = n - 1
    while d % 2 == 0:
        d //= 2

    for a in base:
        t = d
        y = pow(a, t, n)
        while t != n - 1 and y != 1 and y != n - 1:
            y = y * y % n
            t *= 2
        if y != n - 1 and t % 2 == 0:
            return False
    return True


import sys
input = sys.stdin.readline


def print2D(matrix):
    print("\n".join(" ".join(map(str, v)) for v in matrix))


res = []
for _ in range(int(input())):
    n = int(input())
    res.append((n, int(miller_rabin(n))))

print2D(res)
0