結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー shop_oneshop_one
提出日時 2020-05-30 00:51:14
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 5,869 ms / 9,973 ms
コード長 902 bytes
コンパイル時間 99 ms
コンパイル使用メモリ 10,872 KB
実行使用メモリ 9,000 KB
最終ジャッジ日時 2023-08-10 16:16:43
合計ジャッジ時間 13,392 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,928 KB
testcase_01 AC 17 ms
8,828 KB
testcase_02 AC 17 ms
8,932 KB
testcase_03 AC 19 ms
8,712 KB
testcase_04 AC 2,908 ms
9,000 KB
testcase_05 AC 2,674 ms
8,780 KB
testcase_06 AC 370 ms
8,944 KB
testcase_07 AC 371 ms
8,808 KB
testcase_08 AC 342 ms
8,900 KB
testcase_09 AC 5,869 ms
8,732 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import random


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


if __name__ == "__main__":
    x = int(input())
    for i in range(x):
        v = int(input())
        res = judge_miller(v)
        if(res):
            print(str(v)+" 1")
        else:
            print(str(v)+" 0")
0