結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 30 ms
10,752 KB
testcase_02 AC 30 ms
10,624 KB
testcase_03 AC 31 ms
10,624 KB
testcase_04 AC 933 ms
11,136 KB
testcase_05 AC 891 ms
11,136 KB
testcase_06 AC 372 ms
11,264 KB
testcase_07 AC 373 ms
11,392 KB
testcase_08 AC 373 ms
11,136 KB
testcase_09 AC 1,639 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, 325, 9375, 28178, 450775, 9780504, 1795265022]
    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