Числа, имеющие ровно два делителя: единицу и само себя. Все остальные числа, кроме простых и 1, называются составными. Вам нужно посчитать количество составных чисел от l до r, количество делителей которых при этом является простым числом.
Входная строка содержит два целых числа l и r (1 ≤ l ≤ r ≤ 10^14).
Выведите количество таких чисел на отрезке от l до r, включительно.
Пример 1
Ввод 1 9
Вывод 2
Пример 2
Ввод 3 6
Вывод 1
Пример 3
Ввод 6 9
Вывод 1
Assistant
To solve this problem efficiently, we need to identify composite numbers in the range ([l, r]) whose number of divisors is a prime number. The key observation is that the number of divisors (\tau(n)) of a number (n) is calculated using its prime factorization. For a number (n = p_1^{e_1} \times p_2^{e_2} \times \dots \times p_k^{e_k}), the number of divisors is (\tau(n) = (e_1 + 1)(e_2 + 1)\dots (e_k + 1)).
Given that (\tau(n)) is a prime number, it means that the number of divisors is only divisible by 1 and itself, so the product ((e_1 + 1)(e_2 + 1)\dots (e_k + 1)) must be a prime number. This can only occur when there's only one term in the product, meaning that (n) is a power of a prime number, (n = p^e), and the exponent (e) satisfies (e + 1) being a prime number.
Here's the step-by-step algorithm:
Identify exponents (e) such that (e + 1) is prime: We generate possible exponents where (e \geq 2) and (e + 1) is a prime number. This can be done by enumerating primes and subtracting one to find (e).
For each exponent (e):
Calculate the minimum and maximum potential bases (p) such that (p^e) is within the range ([l, r]).
Ensure (p) is a prime number.
For each prime (p) in the calculated range:
Compute (n = p^e).
If (n) is within ([l, r]), increment the count.
Return the total count: After considering all valid (e) and (p), we output the total number of composite numbers found.
Below is the implementation of the algorithm in Python:
python
import sys
import math
import threading
def main():
import bisect
import sys
sys.setrecursionlimit(1 << 25)
l_str, r_str = sys.stdin.read().split()
l = int(l_str)
r = int(r_str)
max_e = 50 # Since r is up to 1e14, exponents larger than 50 will result in p being less than 2
# First, find all exponent e such that e + 1 is prime and e >=2
# Generate all primes up to max_e +1
# Sieve of Eratosthenes up to max_e +1
max_prime = max_e + 1
is_prime = [True] * (max_prime + 1)
is_prime[0] = is_prime[1] = False
for i in range(2, int(max_prime ** 0.5) + 1):
if is_prime[i]:
for j in range(i*i, max_prime +1, i):
is_prime[j] = False
prime_list = [i for i, val in enumerate(is_prime) if val]
exponent_list = [p -1 for p in prime_list if p -1 >= 2 and p -1 <= max_e]
# Now generate all primes up to max_p_max
# Since p^e <= r, p <= r^(1/e)
# The maximum p_max occurs when e=2
max_p_max = int(r ** (1 / 2)) + 1
# Generate all primes up to max_p_max
sieve_limit = max_p_max+1
is_prime_p = [True] * sieve_limit
is_prime_p[0] = is_prime_p[1] = False
for i in range(2, int(sieve_limit ** 0.5) +1):
if is_prime_p[i]:
for j in range(i*i, sieve_limit, i):
is_prime_p[j] = False
prime_p_list = [i for i, val in enumerate(is_prime_p) if val]
count = 0
for e in exponent_list:
e_plus_one = e + 1 # which is prime
p_min = int(l ** (1 / e))
if p_min ** e < l:
p_min +=1
p_max = int(r ** (1 / e))
if p_max ** e > r:
p_max -=1
if p_min > p_max:
continue
# Now get all primes between p_min and p_max
idx_min = bisect.bisect_left(prime_p_list, p_min)
idx_max = bisect.bisect_right(prime_p_list, p_max)
for p in prime_p_list[idx_min: idx_max]:
n = p ** e
if l <= n <= r:
count +=1
print(count)
threading.Thread(target=main).start()
This solution efficiently handles the large input range by precomputing necessary primes and exponents, ensuring that the computation remains within reasonable time and memory constraints.