Processing math: 100%

목차

역팩토리얼

ps
링크acmicpc.net/…
출처BOJ
문제 번호13294
문제명역팩토리얼
레벨골드 3
분류

수학

시간복잡도O(logn)
인풋사이즈n<=10^6
사용한 언어Python
제출기록33272KB / 108ms
최고기록64ms
해결날짜2021/11/18

풀이

코드

코드 1 - 그냥

"""Solution code for "BOJ 13294. 역팩토리얼".

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

import itertools
import math


def main():
    n_fac = input()
    log_n_fac = len(n_fac) - 1 + math.log10(int(n_fac[0]))

    s = 0
    for i in itertools.count(1):
        s += math.log10(i)
        if s >= log_n_fac:
            answer = i
            break

    print(answer)


if __name__ == '__main__':
    main()

코드 2 - 이분탐색

"""Solution code for "BOJ 13294. 역팩토리얼".

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

import math
from teflib import algorithm


def log_fac(x):
    return (math.log(math.factorial(x)) if x < 10 else
            (x * (math.log(x) - 1) + math.log(2 * math.pi * x) / 2))


def main():
    n_fac = input()
    log_n_fac = math.log(10) * (len(n_fac) - 1) + math.log(int(n_fac[0]))

    hi = len(n_fac) + 10
    answer = algorithm.binary_search(1, hi, lambda x: log_fac(x) >= log_n_fac)

    print(answer)


if __name__ == '__main__':
    main()