結果
| 問題 |
No.8030 ミラー・ラビン素数判定法のテスト
|
| ユーザー |
|
| 提出日時 | 2021-03-11 16:54:01 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 443 ms / 9,973 ms |
| コード長 | 956 bytes |
| コンパイル時間 | 352 ms |
| コンパイル使用メモリ | 82,304 KB |
| 実行使用メモリ | 77,312 KB |
| 最終ジャッジ日時 | 2024-11-16 23:37:23 |
| 合計ジャッジ時間 | 2,417 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 10 |
ソースコード
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)