結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー 👑 KazunKazun
提出日時 2020-09-18 02:56:56
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 946 bytes
コンパイル時間 210 ms
コンパイル使用メモリ 81,920 KB
実行使用メモリ 79,104 KB
最終ジャッジ日時 2024-04-29 14:20:01
合計ジャッジ時間 13,838 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 53 ms
54,272 KB
testcase_02 WA -
testcase_03 AC 111 ms
76,672 KB
testcase_04 TLE -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
権限があれば一括ダウンロードができます

ソースコード

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&1==0:
        return False

    x=N-1
    k=0
    while x&1==0:
        k+=1
        x>>=1
    q=(N-1)//(1<<k)

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

        y=pow(m,q,N)

        for i in range(k):
            if (y+1)%N==0:
                return True
            y*=y
            y%=N

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

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