목차

Palindromic Primes

ps
링크acmicpc.net/…
출처BOJ
문제 번호25821
문제명Palindromic Primes
레벨플래티넘 2
분류

정수론

시간복잡도O(sqrt(n) * logn)
인풋사이즈n<=10^12
사용한 언어Python 3.11
제출기록33376KB / 5976ms
최고기록2804ms
해결날짜2023/03/31

풀이

코드

"""Solution code for "BOJ 25821. Palindromic Primes".

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

Tags: [Math] [Number theory]
"""

from teflib import numtheory


def main():
    L, H = [int(x) for x in input().split()]

    answer = 0
    if L <= 2 <= H:
        answer += 1
    if L <= 11 <= H:
        answer += 1
    beg = size = 1
    while beg * size <= H:
        for i in range(beg, beg + size):
            s = str(i)
            n = int(s + s[-2::-1])
            if L <= n <= H and numtheory.is_prime(n):
                answer += 1
        beg += size * 2
        if beg > size * 10:
            beg = size = size * 10

    print(answer)


if __name__ == '__main__':
    main()