結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー はむ吉🐹はむ吉🐹
提出日時 2017-11-05 14:51:38
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 4,287 ms / 9,973 ms
コード長 1,066 bytes
コンパイル時間 166 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 78,848 KB
最終ジャッジ日時 2024-04-28 09:13:32
合計ジャッジ時間 10,501 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 48 ms
53,888 KB
testcase_01 AC 47 ms
53,760 KB
testcase_02 AC 47 ms
54,016 KB
testcase_03 AC 48 ms
54,400 KB
testcase_04 AC 2,173 ms
78,848 KB
testcase_05 AC 1,982 ms
78,336 KB
testcase_06 AC 314 ms
78,464 KB
testcase_07 AC 313 ms
78,208 KB
testcase_08 AC 323 ms
78,848 KB
testcase_09 AC 4,287 ms
78,720 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3

import math
import random


def is_prime_miller_rabin(n, k=50):
    """Determing whether the given integer is prime by the Miller-Rabin test.
    :param int n: The integer to be checked.
    :param int k: The parameter representing the accuracy of the determination.
    :return: Whether n is prime or not.
    :rtype: bool
    """
    assert n > 0
    if n == 2:
        return True
    elif n == 1 or n % 2 == 0:
        return False
    d = n - 1
    s = 0
    while d % 2 == 0:
        d //= 2
        s += 1
    for i in range(k):
        a = random.randint(1, n - 1)
        if pow(a, d, n) != 1:
            for r in range(0, s):
                if pow(a, d * 2 ** r, n) == n - 1:
                    break
            else:
                return False
    return True


def solve(xs):
    return ((x, int(is_prime_miller_rabin(x))) for x in xs)


def main():
    n = int(input())
    xs = (int(input()) for _ in range(n))
    print(*("{} {}".format(r1, r2) for r1, r2 in solve(xs)), sep="\n")


if __name__ == '__main__':
    main()
0