[CF] 230B - T-primes

A simple problem on identifying prime numbers
My Solution
Idea: It’s not hard to realize that we are looking for squared prime numbers. So for each number, we check whether it’s a squared number, if yes, then we check whether the square root is a prime number
1 |
|
Official Solution
Since range of x is x <= 10^12
, square root of x is sqrt(x) < 10^6
So we can try to precompute all possible prime numbers using Sieve of Eratosthenes
Store these prime numbers in a set and square it. Now for each input, we just need to check if it is in the set.
The code can be found here
The time complexity is O(sqrt(m) log log sqrt(m) + n * log(m_prime))
, where m
is the upper bound of x, m_prime
is the number of prime numbers within the 1 to sqrt(m)
.
Thoughts
Sometimes it’s better to precompute stuff.