Author: Vamsi Kavala
Tester: Roman Rubanenko
Editorialist: Bruno Oliveira
Simple Math, Integer Factorization
You are given a very large number represented as a product of N numbers.
Given this number representation, you need to find the number of distinct factors of the original number which is formed by the product of given N numbers.
We can factorize each one of the N given numbers into its prime factors. Then we find the number of occurrences of each prime factor, say they are a1, a2,…aK, if we have K distinct prime factors. Our answer is simply: (a1+1)(a2+1)(…)*(aK+1).
This problem relies on some knowledge of divisor function. Divisor functions returns the number of positive and distinct divisors of a number. Let’s call it d(x).
Some properties of the divisor function:
We now look into some important properties of the divisor function:
For a prime number p, we have d§ = 2, as there are only two numbers which divide a prime number:1 and itself.
Now, it’s a known fact that this function is multiplicative but not completely multiplicative. This means that if two numbers, say, a and b are there such that gcd(a, b) = 1, then the following holds:
d(a*b) = d(a)*d(b).
This allows us to deduce the important relationship, that is the key of solving this problem:
For a prime number, p, we have: d(p^n) = n+1.
Now, it’s easy to understand that all we need to do is to factorize all the N given numbers into its prime factors, and, for each prime factor we also need to count how many times it appears (that is, we need to know the exponent of each prime factor).
Once we have this count with us (which can be done using integer factorization and for example, the set and map data structures, one to guarantee uniqueness of the factors and the other to save the number of occurences for each unique prime factor), all we need to do is to multiply all these numbers plus one together and we will obtain our answer.
As an example, consider the number:
504 = 2^3 * 3^2 * 7^1
The number of distinct divisors of 504 is then (3+1) * (2+1) * (1+1) = 24.
Applying this process to all numbers yields the answer to the problem
Can be found here.
Tester’s solution will be uploaded soon.