結果

問題 No.1498 Factorization from -1 to 1
ユーザー sgswsgsw
提出日時 2021-05-03 23:37:15
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,793 ms / 3,000 ms
コード長 1,768 bytes
コンパイル時間 346 ms
コンパイル使用メモリ 86,860 KB
実行使用メモリ 131,740 KB
最終ジャッジ日時 2023-09-29 19:39:40
合計ジャッジ時間 21,554 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 373 ms
117,280 KB
testcase_01 AC 375 ms
117,448 KB
testcase_02 AC 374 ms
117,536 KB
testcase_03 AC 1,604 ms
125,080 KB
testcase_04 AC 1,658 ms
131,500 KB
testcase_05 AC 1,791 ms
131,320 KB
testcase_06 AC 1,757 ms
131,428 KB
testcase_07 AC 1,774 ms
131,740 KB
testcase_08 AC 1,793 ms
131,360 KB
testcase_09 AC 1,782 ms
131,160 KB
testcase_10 AC 555 ms
123,984 KB
testcase_11 AC 549 ms
123,500 KB
testcase_12 AC 540 ms
123,652 KB
testcase_13 AC 548 ms
123,776 KB
testcase_14 AC 551 ms
123,732 KB
testcase_15 AC 382 ms
117,692 KB
testcase_16 AC 384 ms
117,480 KB
testcase_17 AC 376 ms
117,556 KB
testcase_18 AC 387 ms
117,228 KB
testcase_19 AC 373 ms
117,228 KB
testcase_20 AC 372 ms
117,660 KB
testcase_21 AC 371 ms
117,504 KB
testcase_22 AC 374 ms
117,532 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):
            exp = 0
            while C[j] % p == 0:
                C[j] //= p
                exp += 1
            d[j][p] += exp
        for j in range(p - i, MAXN + 1, p):
            exp = 0
            while C[j] % p == 0:
                C[j] //= p
                exp += 1
            d[j][p] += exp

    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