結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー nagissnagiss
提出日時 2019-09-21 02:27:06
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 862 bytes
コンパイル時間 106 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 11,392 KB
最終ジャッジ日時 2024-04-29 13:27:20
合計ジャッジ時間 3,675 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
10,624 KB
testcase_01 AC 31 ms
10,624 KB
testcase_02 AC 34 ms
10,624 KB
testcase_03 AC 34 ms
10,624 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 701 ms
11,136 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def miller_rabin(n):
    # 参考: http://tjkendev.github.io/procon-library/python/prime/probabilistic.html
    # 確率的素数判定(ミラーラビン素数判定法)
    # 素数なら確実に True を返す、合成数なら確率的に False を返す
    # True が返ったなら恐らく素数で、False が返ったなら確実に合成数である
    primes = [2, 7, 61]  # 64bit: [3, 5, 7, 11, 13, 17, 19, 23]
    if n==2: return True
    if n<=1 or n&1==0: return False
    d = m1 = n-1
    d //= d & -d
    for a in primes:
        if a >= n: return True
        t = d
        y = pow(a, t, n)
        while t!=m1 and y!=1 and y!=m1:
            y = y * y % n
            t <<= 1
        if y!=m1 and t&1==0: return False
    return True

N = int(input())
X = [int(input()) for _ in range(N)]
for x in X:
    print(x, int(miller_rabin(x)))
0