結果

問題 No.1498 Factorization from -1 to 1
ユーザー sgswsgsw
提出日時 2021-05-03 23:28:47
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,527 ms / 3,000 ms
コード長 1,688 bytes
コンパイル時間 313 ms
コンパイル使用メモリ 86,908 KB
実行使用メモリ 137,264 KB
最終ジャッジ日時 2023-09-29 19:32:16
合計ジャッジ時間 26,496 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 344 ms
118,220 KB
testcase_01 AC 344 ms
118,172 KB
testcase_02 AC 351 ms
118,484 KB
testcase_03 AC 2,426 ms
125,928 KB
testcase_04 AC 2,321 ms
136,952 KB
testcase_05 AC 2,433 ms
137,264 KB
testcase_06 AC 2,527 ms
136,920 KB
testcase_07 AC 2,493 ms
136,612 KB
testcase_08 AC 2,455 ms
136,664 KB
testcase_09 AC 2,425 ms
137,028 KB
testcase_10 AC 510 ms
123,688 KB
testcase_11 AC 491 ms
123,932 KB
testcase_12 AC 485 ms
123,532 KB
testcase_13 AC 465 ms
122,888 KB
testcase_14 AC 475 ms
123,724 KB
testcase_15 AC 337 ms
118,664 KB
testcase_16 AC 336 ms
118,116 KB
testcase_17 AC 339 ms
118,524 KB
testcase_18 AC 342 ms
118,648 KB
testcase_19 AC 319 ms
118,592 KB
testcase_20 AC 311 ms
118,732 KB
testcase_21 AC 331 ms
118,292 KB
testcase_22 AC 319 ms
118,496 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import random
from collections import defaultdict
import sys
INF = 1 << 64


def check_composite(n, a, d, s):
    x = pow(a, d, n)
    if x == 1 or x == n - 1:
        return False
    for r in range(1, s):
        x = x * x % n
        if (x == n - 1):
            return False
    return True


def MillerRabin(n, itr=10):
    """
    Primality tests by MillerRabin in O(1).
    """
    if n < 4:
        return (n == 2 or n == 3)
    s, d = 0, n - 1
    while (d & 1 == 0):
        d >>= 1
        s += 1
    for _ in range(itr):
        a = 2 + random.randint(1, INF) % (n - 3)
        if check_composite(n, a, d, s):
            return False
    return True


def input():
    return sys.stdin.readline().rstrip()


MAXN = 100010
C = [i ** 2 + 1 for i in range(MAXN + 1)]


def main():
    """
    verify submission
    """
    Q = int(input())

    Query = [int(input()) for i in range(Q)]

    d = {i: defaultdict(int) for i in range(MAXN + 1)}

    for i in range(1, MAXN + 1):
        if C[i] == 1:
            continue
        p = C[i]
        for j in range(i, MAXN + 1, p):
            while C[j] % p == 0:
                C[j] //= p
                d[j][p] += 1
        for j in range(p - i, MAXN + 1, p):
            while C[j] % p == 0:
                C[j] //= p
                d[j][p] += 1

    for idx in Query:
        fact = []
        v = 1
        for key, value in d[idx].items():
            assert MillerRabin(key, itr=10) == True
            for _ in range(value):
                fact.append(key)
            v *= pow(key, value)
        assert v == idx * idx + 1
        fact.sort()
        print(*fact)

    return 0


if __name__ == "__main__":

    main()
0