結果

問題 No.1498 Factorization from -1 to 1
ユーザー sgswsgsw
提出日時 2021-05-03 23:31:09
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,705 ms / 3,000 ms
コード長 1,681 bytes
コンパイル時間 296 ms
コンパイル使用メモリ 87,248 KB
実行使用メモリ 131,676 KB
最終ジャッジ日時 2023-09-29 19:34:50
合計ジャッジ時間 20,376 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 356 ms
118,408 KB
testcase_01 AC 369 ms
118,472 KB
testcase_02 AC 381 ms
118,448 KB
testcase_03 AC 1,554 ms
123,784 KB
testcase_04 AC 1,610 ms
131,676 KB
testcase_05 AC 1,705 ms
130,880 KB
testcase_06 AC 1,696 ms
131,188 KB
testcase_07 AC 1,688 ms
130,848 KB
testcase_08 AC 1,689 ms
130,280 KB
testcase_09 AC 1,666 ms
130,608 KB
testcase_10 AC 507 ms
123,228 KB
testcase_11 AC 499 ms
122,740 KB
testcase_12 AC 483 ms
122,884 KB
testcase_13 AC 496 ms
123,092 KB
testcase_14 AC 492 ms
123,424 KB
testcase_15 AC 333 ms
118,304 KB
testcase_16 AC 360 ms
118,396 KB
testcase_17 AC 337 ms
118,492 KB
testcase_18 AC 324 ms
118,636 KB
testcase_19 AC 327 ms
118,800 KB
testcase_20 AC 324 ms
118,560 KB
testcase_21 AC 320 ms
118,352 KB
testcase_22 AC 328 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 Code
    """
    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