結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー shop_oneshop_one
提出日時 2020-06-13 15:01:13
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 3,088 ms / 9,973 ms
コード長 877 bytes
コンパイル時間 142 ms
コンパイル使用メモリ 10,832 KB
実行使用メモリ 8,968 KB
最終ジャッジ日時 2023-08-10 16:16:58
合計ジャッジ時間 8,236 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
8,740 KB
testcase_01 AC 18 ms
8,848 KB
testcase_02 AC 19 ms
8,852 KB
testcase_03 AC 19 ms
8,748 KB
testcase_04 AC 1,634 ms
8,944 KB
testcase_05 AC 1,508 ms
8,868 KB
testcase_06 AC 346 ms
8,968 KB
testcase_07 AC 349 ms
8,764 KB
testcase_08 AC 353 ms
8,936 KB
testcase_09 AC 3,088 ms
8,844 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import random


def judge_miller(n, rep):
    if(n == 1):
        return False
    if(n == 2):
        return True
    if(n % 2 == 0):
        return False
    d = n - 1
    while(d & 1 == 0):
        d >>= 1
    for k in range(rep):
        cp_d = d
        a = random.randint(2, n - 1)
        may_prime = False
        x = pow(a, d, n)
        if(x == 1):
            may_prime = True
        while((not may_prime) and x != n - 1 and d != n - 1):
            if(x == n - 1):
                may_prime = True
            d <<= 1
            x = (x * x) % n
        if(x == n - 1):
            may_prime = True
        if(not may_prime):
            return False
        d = cp_d
    return True


n = int(input())
for i in range(n):
    x = int(input())
    t = x
    res = judge_miller(x, 15)
    if(res):
        print(str(t) + " 1")
    else:
        print(str(t) + " 0")
0