結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー neterukunneterukun
提出日時 2021-03-05 13:05:36
言語 PyPy3
(7.3.15)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 618 bytes
コンパイル時間 229 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 77,440 KB
最終ジャッジ日時 2024-04-29 14:52:27
合計ジャッジ時間 2,635 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 44 ms
52,352 KB
testcase_02 AC 44 ms
52,096 KB
testcase_03 AC 44 ms
52,224 KB
testcase_04 AC 278 ms
77,440 KB
testcase_05 AC 266 ms
77,312 KB
testcase_06 AC 180 ms
76,544 KB
testcase_07 AC 180 ms
76,672 KB
testcase_08 AC 179 ms
76,928 KB
testcase_09 AC 381 ms
77,312 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def miller_rabin(n):
    if n == 2:
        return True
    if n == 1 or n % 2 == 0:
        return False

    s = 0
    d = n - 1
    while d & 1 == 0:
        s += 1
        d >>= 1
    for a in (2, 325, 9375, 28178, 450775, 9780504, 1795265022):
        if n <= a:
            break
        x = pow(a, d, n)
        if x == 1:
            continue
        for _ in range(s):
            if x == n - 1:
                break
            x = x * x % n
        else:
            return False

    return True


n = int(input())
x = [int(input()) for i in range(n)]

for val in x:
    print(val, int(miller_rabin(val)))
0