結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー こまるこまる
提出日時 2021-01-02 21:40:47
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 395 ms / 9,973 ms
コード長 832 bytes
コンパイル時間 266 ms
コンパイル使用メモリ 87,120 KB
実行使用メモリ 78,232 KB
最終ジャッジ日時 2023-08-10 16:28:31
合計ジャッジ時間 2,969 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 69 ms
71,040 KB
testcase_01 AC 68 ms
71,208 KB
testcase_02 AC 70 ms
71,468 KB
testcase_03 AC 70 ms
71,180 KB
testcase_04 AC 302 ms
78,000 KB
testcase_05 AC 285 ms
77,828 KB
testcase_06 AC 208 ms
78,232 KB
testcase_07 AC 203 ms
78,108 KB
testcase_08 AC 199 ms
77,688 KB
testcase_09 AC 395 ms
77,708 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def miller_rabin(n, check):
    d, s = n - 1, 0
    while d % 2 == 0:
        d >>= 1
        s += 1
    for a in check:
        if n <= a:
            return True
        a = pow(a, d, n)
        if a == 1:
            continue
        r = 1
        while a != n - 1:
            if r == s:
                return False
            a = a * a % n
            r += 1
    return True


def is_prime32(n):
    return miller_rabin(n, [2, 7, 61])


def is_prime64(n):
    return miller_rabin(n, [2, 325, 9375, 28178, 450775, 9780504, 1795265022])


def is_prime(n):
    if n <= 1:
        return False
    if n <= 3:
        return True
    if n & 1 == 0:
        return False
    if n < 4759123141:
        return is_prime32(n)
    return is_prime64(n)


for _ in range(int(input())):
    x = int(input())
    print(x, int(is_prime(x)))
0