結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー ShirotsumeShirotsume
提出日時 2022-12-12 04:29:38
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 403 ms / 9,973 ms
コード長 1,697 bytes
コンパイル時間 150 ms
コンパイル使用メモリ 82,128 KB
実行使用メモリ 77,452 KB
最終ジャッジ日時 2024-04-23 22:19:55
合計ジャッジ時間 2,404 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 49 ms
54,672 KB
testcase_01 AC 45 ms
55,388 KB
testcase_02 AC 45 ms
54,736 KB
testcase_03 AC 45 ms
55,080 KB
testcase_04 AC 274 ms
77,420 KB
testcase_05 AC 261 ms
77,116 KB
testcase_06 AC 168 ms
77,452 KB
testcase_07 AC 162 ms
76,988 KB
testcase_08 AC 163 ms
77,176 KB
testcase_09 AC 403 ms
76,924 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from math import gcd
def isprime(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,3,325,9375,28178,450775,9780504,1795265022]:
        if a >= n:
            break
        x = pow(a, t, n)
        if x == 1 or x == n - 1:
            continue
        for _ in range(s):
            x = (x * x) % n
            if x == n - 1:
                break
        if x == n - 1:
            continue

        return False
    return True

def Pollad(N):
    if N % 2 == 0:
        return 2
    if isprime(N):
        return N
    def f(x):
        return (x * x + 1) % N
    step = 0

    while True:
        step += 1
        x = step
        y = f(x)
        while True:
            p = gcd(y - x + N, N)
            if p == 0 or p == N:
                break
            if p != 1:
                return p
            x = f(x)
            y = f(f(y))


def Primefact(N):
    if N == 1:
        return []
    q = []
    q.append(N)
    ret = []
    while q:
        now = q.pop()
        if now == 1:
            continue
        p = Pollad(now)
        if p == now:
            ret.append(p)
        else:
            q.append(p)
            q.append(now // p)

    return ret

import sys
from collections import deque, Counter
sys.setrecursionlimit(5 * 10 ** 5)
from pypyjit import set_param
set_param('max_unroll_recursion=-1')
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

n = ii()

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