UVa 10892 - LCM Cardinality

10892 - LCM Cardinality

 * [ ]

Summary
Given a number N, find how many pairs of numbers $$(a,b)$$ with $$a \leq b$$ have least common multiple equal to N. Basically, a counting problem.

Explanation
Consider two numbers a and b, whose prime factorizations are $$2^{a_2} 3^{a_3} ...$$ and $$2^{b_2} 3^{b_3} ...$$, respectively. Then their LCM is $$2^{max(a_2,b_2)} 3^{max(a_3,b_3)} ...$$ This is the insight that lets us solve the problem.

So factorize N: $$p_1^{n_1} p_2^{n_2} ... p_k^{n_k}$$. Now say that we have a and b such that LCM(a,b) = N. Then for each i, at least one of $$a_i$$ and $$b_i$$ is equal to $$n_i$$. This would suggest that there are $$2n_i + 1$$ possibilities for each prime factor, and that if we multiply them all together, we're done. Unfortunately, we run into the issue of overcounting. Thus we need to modify this approach somewhat. We will solve this problem incrementally and recursively.

For a given N, call the smallest prime factor $$p_1$$, and say that $$n_1$$ is the largest power of $$p_1$$ that divides N. Break the problem into two cases. In case one, we set $$a_1 < b_1 = n_1$$. This gives us $$n_1$$ possibilities for the first number, and for the rest, we can simply take the product of $$(2n_i + 1)$$ for $$i > 1$$. (Verify that this does not count any pairs more than once.) In the other case, we have $$a_1 = b_1 = n_1$$. In this case, we just need to solve the same problem, but for $$\frac{N}{p_1^{n_1}}$$. (Similarly, verify that these two cases together do not miss any pairs.) The base case of our recursion is when N = 1, in which case we just return 1.

There is another method for calculating the Answer. When we take the product of $$(2n_i + 1)$$,for all i, this value actually counts each distinct pair twice, except the last pair(N,N), it's counted once(Think why, simulate small cases). so we can simply add 1 to that product, and divide it by 2,to get the actual answer.

Implementations
There does not need to be actual recursion, because we already know the factorization of $$\frac{N}{p_1^{n_1}}$$.

Input
1 2 12 24 101101291 0

Output
1 1 2 2 12 8 24 11 101101291 5