結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー kappybarkappybar
提出日時 2020-04-29 16:11:10
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 5,460 ms / 9,973 ms
コード長 526 bytes
コンパイル時間 80 ms
コンパイル使用メモリ 12,544 KB
実行使用メモリ 11,136 KB
最終ジャッジ日時 2024-04-28 09:30:28
合計ジャッジ時間 12,887 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
11,136 KB
testcase_01 AC 33 ms
11,136 KB
testcase_02 AC 34 ms
11,136 KB
testcase_03 AC 34 ms
11,136 KB
testcase_04 AC 2,769 ms
11,008 KB
testcase_05 AC 2,468 ms
11,136 KB
testcase_06 AC 447 ms
11,136 KB
testcase_07 AC 449 ms
11,008 KB
testcase_08 AC 454 ms
11,136 KB
testcase_09 AC 5,460 ms
11,008 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def Miller_Rabin(x):
    import random
    if x < 2 : return False
    if x == 2 : return True
    if x & 1 == 0: return False

    d = x - 1
    while d%2 ==0 : d //= 2

    for _ in range(20):
        p = random.randint(1,x-1)
        a = pow(p,d,x)
        s = d

        while s != x-1 and a != 1 and a != x-1:
            a = (a * a)%x
            s <<= 1

        if a != x-1 and s%2 == 0: return False

    return True

n = int(input())

for _ in range(n):
    x = int(input())
    print(x,1 if Miller_Rabin(x) else 0)
0