結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー ShirotsumeShirotsume
提出日時 2022-04-22 03:18:57
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,246 bytes
コンパイル時間 992 ms
コンパイル使用メモリ 87,184 KB
実行使用メモリ 79,048 KB
最終ジャッジ日時 2023-09-05 10:09:21
合計ジャッジ時間 3,992 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
71,404 KB
testcase_01 AC 72 ms
71,412 KB
testcase_02 AC 72 ms
71,444 KB
testcase_03 AC 72 ms
71,344 KB
testcase_04 WA -
testcase_05 AC 334 ms
78,388 KB
testcase_06 AC 230 ms
79,048 KB
testcase_07 AC 223 ms
78,504 KB
testcase_08 AC 219 ms
78,100 KB
testcase_09 AC 480 ms
78,144 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from math import gcd
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, 3, 5, 7, 11, 13, 17, 19, 23]:
        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 pollad(N):
    if N % 2 == 0:
        return 2
    if is_prime(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 prime_fact(N):
    if N == 1:
        return []
    p = pollad(N)
    if p == N:
        return [p]
    return prime_fact(p) + prime_fact(N // p)

n = int(input())

for _ in range(n):
    x = int(input())
    if is_prime(x):
        print(x, 1)
    else:
        print(x, 0)

0