Codeforces
CF Step
Youtube Linkedin Discord Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Count Arrays with Fixed Product

Find the number of arrays of length L containing positive integers whose product is equal to P.

L105 and P1010

This is a subproblem of ABC370G: Divisible by 3. You can submit it here

First, let’s solve an easy version. Assume P=p1α1

We should have a1a2aL=p1α1. It is clear that each ai can have only have p1 as its prime factor, although the multiplicity of the exponent might differ. However, their product should contain exactly α1 multiplicity.

So, let’s assume that ai=p1xi. Then, we should have p1x1p1x2p1xL=p1α1 So we are looking for the number of non-negative solution to: x1+x2++xL=α1

From Stars and Bars, the number of distinct solution is equal to (L+α11α1). If you are not familiar with Stars and Bars, you can read about it here but don’t read too much into it, else you’d end up confusing yourself. For a long time, I was afraid of problems that involved Stars and Bars, mainly because I used to apply Stars and Bars combinatorics from scratch for each problem.

But if you take the combinatorics for granted, and just memorize the formula for x1+x2++xn=sum is (n+sum1sum), you will be able to solve all problems that involve Stars and Bars. Why? Because there’s a bijection between them, and every problem that can be solved using Stars and Bars can also be reduced to counting the number of distinct integral solution of a given equation. This abstraction is extremely useful for solving Combinatorial problems.

What if P had 2 different prime factors? Suppose P=p1α1p2α2. Since a1a2aL=p1α1p2α2, it is clear that each ai will have atmost 2 prime factors, i.e, p1 and p2.

Assume ai=p1xip2yi. Then (p1x1p1x2p1xL)(p2y1p2y2p2yL)=p1α1p2α2 So we are looking for the number of non-negative solution to x1+x2++xL=α1 y1+y2++yL=α2

Notice that all xi are correlated to each other, all yi are correlated to each other (because their sum is fixed). However, xi does not interact with yj. Hence, they are independent. So the total number of ways is equal to the product of the number of ways to find X=(x1,x2,,xL) and Y=(y1,y2,,yL), which is nothing but (L+α11α1)(L+α21α2)

What if P=p1α1p2α2pkαk? Note that the prime factors do not interact with each other. Therefore, the number of distinct arrays with product P is equal to i=1k(L+αi1αi)

Since N1010, you can compute prime factorization in O(n). Notice that each αi cannot be bigger than log(N). Why?

Therefore, factorials can be precomputed to compute (NR) fast.

You can submit this problem here.

Code

Multiplicative Functions

Suppose f(N)=i=1k(L+αi1αi) and f(M)=i=1k(L+βi1βi)

If N and M are co-prime, notice that f(NM)=f(N)f(M)

This is because the prime factorization of co-prime integers have nothing in common, therefore, the product will contain all prime factors in each number. Hence, none of the primes interacts with each other, even in the multiplication.

Functions that obey this property for co-prime inputs are known as Multiplicative Functions.

Prefix Sum of Multiplicative Functions

How do you find out the number of arrays of length L with product N?

We are simply looking for f(1)+f(2)+f(3)+f(N)

Naively computing it would lead to TLE, as N1010.

However, since this is a multiplicative function, there exists a dynamic programming solution to compute the prefix sum of multiplicative functions efficiently. I might add another blog on this topic in future, but feel free to google the keyword and do your own research.

This technique is needed to gain full points for ABC370G: Divisible by 3.

Practice Problem

If you liked the contents of this blog, you might like problem How many arrays from Codechef.