Given L and R very large numbers (10^18) , how do i find count of numbers between L and R such that numbers have atleast one prime factors from 1 to N.
Note : N can be at MAX 50
Given L and R very large numbers (10^18) , how do i find count of numbers between L and R such that numbers have atleast one prime factors from 1 to N.
Note : N can be at MAX 50
| № | Пользователь | Рейтинг |
|---|---|---|
| 1 | Benq | 3676 |
| 2 | jiangly | 3670 |
| 3 | Kevin114514 | 3657 |
| 4 | maroonrk | 3355 |
| 5 | Um_nik | 3353 |
| 6 | squareOf105 | 3346 |
| 7 | tourist | 3343 |
| 8 | strapple | 3324 |
| 9 | heuristica | 3322 |
| 10 | jiangbowen | 3291 |
| Страны | Города | Организации | Всё → |
| № | Пользователь | Вклад |
|---|---|---|
| 1 | Qingyu | 158 |
| 2 | nik_exists | 150 |
| 2 | maspy | 150 |
| 4 | AmShZ | 143 |
| 5 | Um_nik | 141 |
| 6 | Errichto | 139 |
| 7 | adamant | 137 |
| 8 | BledDest | 133 |
| 8 | maroonrk | 133 |
| 10 | qwexd | 129 |
| Название |
|---|



Let's use n=5 and let count(i) be the number of multiples of i in the range L <= r <= R
Naively count the multiples of the 2,3,5
ans += count(2) + count(3) + count(5)
Now, subtract the multiples of the numbers that you counted twice (the numbers that have at least 2 primes as divisors)
ans -= count(2*3) + count(2*5) + count(3*5)
Now, for 2*3*5, you counted these numbers 3 times in the first step and subtracted 3 times in the second step so you just need to do:
ans += count(2*3*5)
More generally, you will take every number that is the product of some number of primes lesser or equal to n (at least 1 prime) and if there is an odd number of primes, you add. If there is an even number of primes, you subtract. The complexity will be O(2^number_of_primes_until(n)) since count(i) is constant.
Edit: sorry, it isn't as simple as subtract/add but the thought is this.
Use principle of inclusion and exclusion. Key observations are:
Let count(A) be the count of numbers between L and R such that numbers are divisible by that prime number A.
You basically want to find the value of expression count(2
U3U....Ulast prime number(<=n)), TheUis the union operator.The expression above means that you want to calculate numbers between L and** R** such that numbers are divisible by at least one of those prime numbers, which is basically the principle of inclusion and exclusion
Using the above, you have max of 2^15 subsets of calculations that you have to do. Each of which will require O(1) time, since it will require just two divisions and a subtraction.