목차

GCD!

ps
링크acmicpc.net/…
출처BOJ
문제 번호7806
문제명GCD!
레벨골드 3
분류

정수론

시간복잡도O(sqrt(k) + logn)
인풋사이즈k<=10^9, n<=10^9
사용한 언어Python 3.13
제출기록32412KB / 68ms
최고기록68ms
해결날짜2026/01/26

풀이

코드

"""Solution code for "BOJ 7806. GCD!".

- Problem link: https://www.acmicpc.net/problem/7806
- Solution link: http://www.teferi.net/ps/problems/boj/7806

Tags: [number theory]
"""

import sys
from teflib import psutils
from teflib import numtheory


def exponent_of_prime_in_factorial(n, p):
    """Compute largest power of a prime p that divides n!, in O(logn/logp)."""
    count = 0
    while n:
        n //= p
        count += n
    return count


@psutils.run_until_eof
def main():
    n, k = [int(x) for x in sys.stdin.readline().split()]
    factorization = numtheory.prime_factorization_small(k)

    answer = 1
    for p, e in factorization.items():
        e2 = exponent_of_prime_in_factorial(n, p)
        answer *= pow(p, min(e, e2))

    print(answer)


if __name__ == '__main__':
    main()