結果
問題 | No.3030 ミラー・ラビン素数判定法のテスト |
ユーザー | hari64 |
提出日時 | 2021-07-27 22:36:04 |
言語 | PyPy3 (7.3.13) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,167 bytes |
コンパイル時間 | 347 ms |
コンパイル使用メモリ | 87,080 KB |
実行使用メモリ | 78,752 KB |
最終ジャッジ日時 | 2023-10-01 03:10:44 |
合計ジャッジ時間 | 3,438 ms |
ジャッジサーバーID (参考情報) |
judge15 / judge12 |
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 67 ms
71,496 KB |
testcase_01 | AC | 69 ms
71,524 KB |
testcase_02 | AC | 74 ms
71,548 KB |
testcase_03 | AC | 69 ms
71,340 KB |
testcase_04 | WA | - |
testcase_05 | AC | 340 ms
78,256 KB |
testcase_06 | AC | 225 ms
78,516 KB |
testcase_07 | AC | 227 ms
78,752 KB |
testcase_08 | AC | 223 ms
78,340 KB |
testcase_09 | AC | 496 ms
78,668 KB |
ソースコード
def is_prime(n: int) -> bool: # ミラー–ラビン素数判定法 # https://qiita.com/srtk86/items/609737d50c9ef5f5dc59 # http://miller-rabin.appspot.com/ assert isinstance(n, int) and 0 < n if n == 2: return True if n == 1 or n & 1 == 0: return False d = (n - 1) >> 1 while d & 1 == 0: d >>= 1 # n-1=(2**s)*d (dは奇数) if n <= 4759123141: L = [a for a in (2, 7, 61) if a < n] elif n <= 2**64: L = [a for a in (2, 7, 61, 325, 9375, 28178, 450775, 9780504, 1795265022)] if n >= 2**64: from random import randint # これだけあればまず誤判定しない L += [randint(1, n-1) for _ in range(20)] for a in L: # nが素数ならばa^d≡1 (mod p) もしくは t = d # a^((2^r)*d)≡-1 (mod p) が成立すべき y = pow(a, t, n) while t != n - 1 and y != 1 and y != n - 1: y = (y * y) % n t <<= 1 if y != n - 1 and t & 1 == 0: return False else: return True n = int(input()) for _ in range(n): x = int(input()) print(x, int(is_prime(x)))