結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
11,136 KB
testcase_01 AC 36 ms
11,264 KB
testcase_02 AC 36 ms
11,392 KB
testcase_03 AC 37 ms
11,264 KB
testcase_04 WA -
testcase_05 AC 905 ms
11,136 KB
testcase_06 AC 411 ms
11,264 KB
testcase_07 AC 407 ms
11,136 KB
testcase_08 AC 411 ms
11,136 KB
testcase_09 AC 1,660 ms
11,264 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from random import randrange
from math import gcd

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]
    if n < 1050535501:
        bases = [336781006125, 9639812373923155]
    if n < 350269456337:
        bases = [4230279247111683200, 14694767155120705706, 16641139526367750375]
    if n < 55245642489451:
        bases = [2, 141889084524735, 1199124725622454117, 11096072698276303650]
    if n < 7999252175582851:
        bases = [2, 4130806001517, 149795463772692060, 186635894390467037, 3967304179347715805]
    if n < 585226005592931977:
        bases = [2, 123635709730000, 9233062284813009, 43835965440333360, 761179012939631437, 1263739024124850375]
    else:
        bases = [2, 325, 9375, 28178, 450775, 9780504, 1795265022]
    for a in bases:
        if a == n:
            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 i in range(int(input())):
    x = int(input())
    print(x, is_prime(x))
0