Facebook Hacker cup 2015, Round 1 Solution
Homework
The problem introduces the term primacity as:
Let Im an integer positive and Pn a prime number, the primacity of Im is the amount of Pn which divides it.
For example: Let I=550, its primacity is 3 becuase 2,5 and 11 divide it.
The problem ask us: Given 3 numbers A,B and K, how many integers in the inclusive range [A,B] have a primacity of exactly K
Constraints:
1≤T≤100 , Let T the number of test cases
2≤A≤B≤107
1≤K≤109
Clearly, we need to have a list of primes numbers for used later, we can use a modified of the Sieve of Eratosthenes. Let L an array with length 107+1 with initialized values of 0, the principle idea here is that each time that we found a Pn in the sieve we increment by 1 to L[Pn] thought L[iPn], where i is a number lesser than 107Pn. Then, when we ask if Im in the interval [A,B] has primacity K, just we need to check out if Im==L[Im]. After that only we increment an r by 1 each time that last statement is true for get the final answer. The complexity of this task is the same than the Sieve: (nlogn) for the integers 1 to n.
Read full article from Facebook Hacker cup 2015, Round 1 Solution
No comments:
Post a Comment