Editorial for COCI '11 Contest 4 #5 Broj
Submitting an official solution before solving the problem yourself is a bannable offence.
To solve this for large values of we will use a modification of the sieve of Eratosthenes. The size of our sieve will be . Integers in the sieve represent multiples of . During the execution of this algorithm, we can find the smallest prime factors or mark only multiples of prime numbers smaller than .
For smaller values of we can binary search through , again looking at these numbers as the corresponding multiples of . For some number, we must find the number of integers not greater and relatively prime with that number. We can do this by using the inclusion-exclusion principle with prime numbers less than .
With careful implementation, this solution can work for much larger values of than requested for this subtask.
We can also solve this task for smaller values of by making use of the periodic behaviour of the smallest prime factors. Let be the smallest prime factor of , the prime number, and the product of the first primes. For , holds. So it's enough to know for in order to find the prime whose smallest prime factor is .
Comments