結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー JashinchanJashinchan
提出日時 2022-08-26 12:49:57
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 1,230 bytes
コンパイル時間 389 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 10,880 KB
最終ジャッジ日時 2024-04-21 15:49:50
合計ジャッジ時間 5,697 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

def is_prime(n):
    if n == 2:
        return 1
    if n == 1 or n % 2 == 0:
        return 0
    m = n - 1
    lsb = m & -m
    s = lsb.bit_length() - 1
    d = m // lsb
    if n < 341531:
        bases = [9345883071009581737]
    elif n < 1050535501:
        bases = [336781006125, 9639812373923155]
    elif n < 350269456337:
        bases = [4230279247111683200, 14694767155120705706, 16641139526367750375]
    elif n < 55245642489451:
        bases = [2, 141889084524735, 1199124725622454117, 11096072698276303650]
    elif n < 7999252175582851:
        bases = [2, 4130806001517, 149795463772692060, 186635894390467037, 3967304179347715805]
    elif n < 585226005592931977:
        bases = [2, 123635709730000, 9233062284813009, 43835965440333360, 761179012939631437, 1263739024124850375]
    else:
        bases = [2, 325, 9375, 28178, 450775, 9780504, 1795265022]
    for a in bases:
        if n <= a:
            continue
        x = pow(a, d, n)
        r = 0
        if x == 1:
            continue
        while x != m:
            x = pow(x, 2, n)
            r += 1
            if x == 1 or r == s:
                return 0
    return 1

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