結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー ShirotsumeShirotsume
提出日時 2022-04-21 03:14:02
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 396 ms / 9,973 ms
コード長 1,575 bytes
コンパイル時間 149 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 78,208 KB
最終ジャッジ日時 2024-04-28 09:50:17
合計ジャッジ時間 2,349 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
55,424 KB
testcase_01 AC 46 ms
55,296 KB
testcase_02 AC 46 ms
55,680 KB
testcase_03 AC 45 ms
56,064 KB
testcase_04 AC 270 ms
77,628 KB
testcase_05 AC 277 ms
77,764 KB
testcase_06 AC 164 ms
78,208 KB
testcase_07 AC 152 ms
78,164 KB
testcase_08 AC 159 ms
77,976 KB
testcase_09 AC 396 ms
77,824 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = lambda: sys.stdin.readline().rstrip()
ii = lambda: int(input())
mi = lambda: map(int, input().split())
li = lambda: list(mi())
INF = 2 ** 63 - 1
mod = 998244353

from math import gcd
import random
from collections import defaultdict
def is_prime(n):
    if n <= 2:
        return n == 2
    if n % 2 == 0:
        return False
    s = 0
    t = n - 1
    while t % 2 == 0:
        s += 1
        t //= 2
    
    for a in [2, 7, 61, 325, 9375, 28178, 450775, 9780504, 1795265022]:
        if a >= n:
            break
        x = pow(a, t, n)
        if x == 1 or x == n - 1:
            continue
        for i in range(s):
            x = (x * x) % n
            if x == n - 1:
                break
        if x == n - 1:
            continue

        return False
    return True

def prime_fact(N):
    res=defaultdict(int)
    if N==1:
        return res
    p=2
    while(p<=10**4 and N>1):
        if N%p==0:
            while(N%p==0):
                res[p]+=1
                N//=p
        p+=1
    while(N>1):
        if is_prime(N):
            res[N]+=1
            break
        x=random.randrange(N)
        y=(x*x+1)%N
        i=1
        while(True):
            d=gcd(abs(x-y),N)
            if d==1:
                i+=1
            elif d==N:
                res[N]+=1
                return res
            else:
                res[d]+=1
                N//=d
                break
            x=(x*x+1)%N
            y=(y*y+1)%N
            y=(y*y+1)%N
    return res

for _ in range(ii()):
    n = ii()
    print(n, int(is_prime(n)))
0