結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー masa_aamasa_aa
提出日時 2021-03-11 16:54:01
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 491 ms / 9,973 ms
コード長 956 bytes
コンパイル時間 277 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 77,696 KB
最終ジャッジ日時 2024-04-28 09:42:38
合計ジャッジ時間 2,633 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
51,840 KB
testcase_01 AC 41 ms
52,096 KB
testcase_02 AC 42 ms
52,224 KB
testcase_03 AC 41 ms
51,840 KB
testcase_04 AC 308 ms
77,056 KB
testcase_05 AC 298 ms
77,696 KB
testcase_06 AC 153 ms
76,672 KB
testcase_07 AC 147 ms
76,672 KB
testcase_08 AC 146 ms
77,312 KB
testcase_09 AC 491 ms
77,056 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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