結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー 山本信二山本信二
提出日時 2022-05-29 23:46:29
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 2,241 ms / 9,973 ms
コード長 1,389 bytes
コンパイル時間 94 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 11,008 KB
最終ジャッジ日時 2024-04-28 09:50:41
合計ジャッジ時間 6,477 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 25 ms
11,008 KB
testcase_01 AC 28 ms
11,008 KB
testcase_02 AC 27 ms
11,008 KB
testcase_03 AC 28 ms
11,008 KB
testcase_04 AC 1,203 ms
10,880 KB
testcase_05 AC 1,175 ms
11,008 KB
testcase_06 AC 369 ms
11,008 KB
testcase_07 AC 374 ms
10,880 KB
testcase_08 AC 373 ms
10,880 KB
testcase_09 AC 2,241 ms
11,008 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, 31, 37]:
        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 []
    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


q = int(input())

for _ in range(q):
    a = int(input())
    print(a, int(is_prime(a)))
0