結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー JashinchanJashinchan
提出日時 2022-08-24 18:36:46
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 1,668 bytes
コンパイル時間 104 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 21,504 KB
最終ジャッジ日時 2024-04-20 07:50:53
合計ジャッジ時間 12,171 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 27 ms
10,880 KB
testcase_02 AC 26 ms
10,880 KB
testcase_03 AC 26 ms
10,880 KB
testcase_04 TLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
権限があれば一括ダウンロードができます

ソースコード

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_prime1(n):
    # < 5329
    return miller_rabin(n, [377687])


def is_prime2(n):
    # < 19471033
    return miller_rabin(n, [2, 299417])


def is_prime3(n):
    # < 4759123141
    return miller_rabin(n, [2, 7, 61])


def is_prime4(n):
    # < 1122004669633
    return miller_rabin(n, [2, 13, 23, 1662803])


def is_prime5(n):
    # < 3071837692357849
    return miller_rabin(n, [2, 75088, 642735, 203659041, 3613982119])


def is_prime6(n):
    # < 585226005592931977
    return miller_rabin(n, [2, 123635709730000, 9233062284813009, 43835965440333360, 761179012939631437, 1263739024124850375])


def is_prime7(n):
    # < 2 ** 64
    return miller_rabin(n, [2, 3, 5, 7, 325, 9375, 28178, 450775, 9780504, 1795265022])


def is_prime(n):
    """ https://miller-rabin.appspot.com/ """
    if n < 5329:
        return is_prime1(n)
    if n < 19471033:
        return is_prime2(n)
    if n < 4759123141:
        return is_prime3(n)
    if n < 1122004669633:
        return is_prime4(n)
    if n < 3071837692357849:
        return is_prime5(n)
    if n < 585226005592931977:
        return is_prime6(n)
    if n < 2 ** 64:
        return is_prime7(n)
    return miller_rabin(n, *range(2, n - 1))


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