Editorial for DMOPC '19 Contest 6 P2 - Grade 10 Math


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Author: Tzak

Let A and cnt be the list of prime factors and the list of their respective frequencies in the prime factorization of a. The elements of A can be obtained through prime factorizing a. Now, for each Ai in A, we find ni: the greatest integer value that cnti can be multiplied by, and remain not greater than the number of times Ai appears in the prime factorization of b!. The answer is the minimum over all ni.

For example, consider Sample Input 1, with a=8, b=849. After prime factorizing, A=[2] and cnt=[3]. Now consider how many times 2 occurs in the expansion of 849!=1×2××849. Every two numbers in the expansion of 849!=1×2× have 2 as a factor, and there are 8492=424 such numbers. However, for elements which are divisible by 22=4 in the expansion, there is still one 2 which we have not yet considered. Similarly, there are 8494=212 such elements. For elements which are divisible by 23=8, there is yet one more 2 which we have not considered, and the pattern continues. We will find that in total, there are 8492+8494+8498+84916++849512=424+212+106+53+26+13+6+3+1=844 occurrences of 2 in the prime factorization of 849!. Now, we find ni, which is 8443=241. Since there is only one such ni, this is our answer.

Note: x means x, rounded down to the nearest integer.

Pseudocode:

Copy
read a, b

(prime factorize a)

for i in [2, sqrt(a)]
    if a is divisible by i
        append i to A
    while a is divisible by i
        a /= i
        cnt[i]++

if a != 1
    insert a into A
    cnt[a]++

(find all n_i)

n = MAXIMUM_VALUE
for A_i in A
    occurrences = 0
    looking_for = A_i
    while b / looking_for != 0
        occurrences += b / looking_for
        looking_for *= A_i
    n_i = occurrences / cnt[A_i]
    n = min(n, n_i)

print n

Time complexity: O(n+log2nloglogn)


Comments

There are no comments at the moment.