結果

問題 No.300 平方数
ユーザー hiragnhiragn
提出日時 2022-11-19 00:33:29
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 35 ms / 1,000 ms
コード長 1,542 bytes
コンパイル時間 189 ms
コンパイル使用メモリ 12,544 KB
実行使用メモリ 11,392 KB
最終ジャッジ日時 2024-09-20 04:48:51
合計ジャッジ時間 3,041 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 43
権限があれば一括ダウンロードができます

ソースコード

diff #

from math import gcd
import random
from collections import defaultdict
from functools import reduce
from operator import mul


def is_prime(n):
    if n == 2:
        return True
    if n == 1 or n & 1 == 0:
        return False
    d = (n - 1) >> 1
    while d & 1 == 0:
        d >>= 1

    for k in range(100):
        a = random.randint(1, n - 1)
        t = d
        y = pow(a, t, n)
        while t != n - 1 and y != 1 and y != n - 1:
            y = (y * y) % n
            t <<= 1
        if y != n - 1 and t & 1 == 0:
            return False
    return True


def prime_fact(N):
    res = defaultdict(int)
    if N == 1:
        return res
    p = 2
    while p <= 10 ** 4 and N > 1:
        if N % p == 0:
            while N % p == 0:
                res[p] += 1
                N //= p
        p += 1
    while N > 1:
        if is_prime(N):
            res[N] += 1
            break
        x = random.randrange(N)
        y = (x * x + 1) % N
        i = 1
        while True:
            d = gcd(abs(x - y), N)
            if d == 1:
                i += 1
            elif d == N:
                res[N] += 1
                return res
            else:
                res[d] += 1
                N //= d
                break
            x = (x * x + 1) % N
            y = (y * y + 1) % N
            y = (y * y + 1) % N
    return res


def main():
    n = int(input())

    lst = [1] + [p for p, k in prime_fact(n).items() if k % 2 == 1]
    ans = reduce(mul, lst)
    print(ans)


if __name__ == "__main__":
    main()
0