結果

問題 No.1498 Factorization from -1 to 1
ユーザー sgswsgsw
提出日時 2021-05-03 23:29:27
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,801 ms / 3,000 ms
コード長 1,687 bytes
コンパイル時間 306 ms
コンパイル使用メモリ 86,992 KB
実行使用メモリ 131,076 KB
最終ジャッジ日時 2023-09-29 19:33:09
合計ジャッジ時間 21,290 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 375 ms
118,456 KB
testcase_01 AC 375 ms
118,376 KB
testcase_02 AC 372 ms
118,536 KB
testcase_03 AC 1,581 ms
124,436 KB
testcase_04 AC 1,646 ms
130,672 KB
testcase_05 AC 1,773 ms
130,992 KB
testcase_06 AC 1,762 ms
130,592 KB
testcase_07 AC 1,772 ms
131,076 KB
testcase_08 AC 1,778 ms
130,352 KB
testcase_09 AC 1,801 ms
130,464 KB
testcase_10 AC 557 ms
123,524 KB
testcase_11 AC 548 ms
123,336 KB
testcase_12 AC 547 ms
123,108 KB
testcase_13 AC 533 ms
123,152 KB
testcase_14 AC 538 ms
123,468 KB
testcase_15 AC 369 ms
118,308 KB
testcase_16 AC 374 ms
118,552 KB
testcase_17 AC 376 ms
118,596 KB
testcase_18 AC 365 ms
118,592 KB
testcase_19 AC 368 ms
118,804 KB
testcase_20 AC 373 ms
118,292 KB
testcase_21 AC 372 ms
118,156 KB
testcase_22 AC 375 ms
118,516 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=5) == 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