結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー TakaTaka
提出日時 2023-10-04 13:36:19
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 2,228 ms / 9,973 ms
コード長 773 bytes
コンパイル時間 246 ms
コンパイル使用メモリ 10,824 KB
実行使用メモリ 8,924 KB
最終ジャッジ日時 2023-10-04 13:36:26
合計ジャッジ時間 6,729 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 20 ms
8,788 KB
testcase_01 AC 19 ms
8,848 KB
testcase_02 AC 19 ms
8,836 KB
testcase_03 AC 20 ms
8,764 KB
testcase_04 AC 1,278 ms
8,848 KB
testcase_05 AC 1,132 ms
8,804 KB
testcase_06 AC 359 ms
8,808 KB
testcase_07 AC 368 ms
8,884 KB
testcase_08 AC 365 ms
8,744 KB
testcase_09 AC 2,228 ms
8,924 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import random

def is_prime_miller_rabin(n, k=10):
    if n <= 1:
        return False
    if n <= 3:
        return True

    # n - 1を (2^r) * d に分解する
    r, d = 0, n - 1
    while d % 2 == 0:
        r += 1
        d //= 2

    # ミラーラビンテストをk回繰り返す
    for _ in range(k):
        a = random.randint(2, n - 2)
        x = pow(a, d, n)

        if x == 1 or x == n - 1:
            continue

        for _ in range(r - 1):
            x = pow(x, 2, n)
            if x == n - 1:
                break
        else:
            return False
    return True
    
N = int(input())

for i in range(N):
    num = int(input())
    if is_prime_miller_rabin(num):
        ans = 1
    else:
        ans = 0
        
    print(num,ans,sep=' ')
0