結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー 👑 KazunKazun
提出日時 2020-09-18 03:20:01
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,163 ms / 9,973 ms
コード長 994 bytes
コンパイル時間 277 ms
コンパイル使用メモリ 82,688 KB
実行使用メモリ 79,232 KB
最終ジャッジ日時 2024-04-28 09:37:04
合計ジャッジ時間 3,854 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
53,632 KB
testcase_01 AC 39 ms
53,888 KB
testcase_02 AC 39 ms
54,144 KB
testcase_03 AC 39 ms
54,400 KB
testcase_04 AC 623 ms
78,720 KB
testcase_05 AC 566 ms
78,464 KB
testcase_06 AC 161 ms
79,232 KB
testcase_07 AC 172 ms
78,592 KB
testcase_08 AC 176 ms
78,592 KB
testcase_09 AC 1,163 ms
78,592 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#Miller-Rabinの素数判定法
def Miller_Rabin_Primality_Test(N,Times=20):
    """Miller-Rabinによる整数Nの素数判定を行う.

    N:整数
    ※:Trueは正確にはProbably Trueである(Falseは確定False).
    """
    from random import randint as ri

    if N==2:
        return True

    if N==1 or N%2==0:
        return False

    q=N-1
    k=0
    while q&1==0:
        k+=1
        q>>=1

    for _ in range(Times):
        m=ri(2,N-1)
        y=pow(m,q,N)
        if y==1:
            continue

        flag=True
        for i in range(k):
            if (y+1)%N==0:
                flag=False
                break

            y*=y
            y%=N

        if flag:
            return False

    return True
#================================================
N=int(input())
Y=[0]*N
for i in range(N):
    x=int(input())
    if Miller_Rabin_Primality_Test(x,30):
        Y[i]="{} {}".format(x,1)
    else:
        Y[i]="{} {}".format(x,0)

print("\n".join(map(str,Y)))
0