結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー ra5anchorra5anchor
提出日時 2024-04-20 05:40:31
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 547 ms / 9,973 ms
コード長 684 bytes
コンパイル時間 153 ms
コンパイル使用メモリ 82,496 KB
実行使用メモリ 78,656 KB
最終ジャッジ日時 2024-04-20 05:40:35
合計ジャッジ時間 3,005 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 33 ms
52,464 KB
testcase_01 AC 34 ms
53,436 KB
testcase_02 AC 34 ms
52,872 KB
testcase_03 AC 37 ms
53,084 KB
testcase_04 AC 332 ms
77,368 KB
testcase_05 AC 330 ms
77,704 KB
testcase_06 AC 199 ms
77,284 KB
testcase_07 AC 186 ms
77,464 KB
testcase_08 AC 216 ms
78,172 KB
testcase_09 AC 547 ms
78,656 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import math
def suspect(a, t, n):
    x = pow(a, t, n)
    n1 = n - 1
    while t != n1 and x != 1 and x != n1:
        x = pow(x, 2, n)
        t <<= 1
    return t & 1 or x == n1
def miller_rabin(n):
    if n == 2:
        return True
    if n < 2 or n % 2 == 0:
        return False
    d = (n - 1) >> 1
    while d & 1 == 0:
        d >>= 1
    check_list = (2, 7, 61) if n < 2 ** 32 else (2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37)
    for i in check_list:
        if i >= n:
            break
        if not suspect(i, d, n):
            return False
    return True

n = int(input())
for i in range(n):
    x = int(input())
    ans = int(miller_rabin(x))
    print(x, ans)



0