Count Ways to Make Array With Product - Problem

Imagine you're a mathematician tasked with finding all possible ways to arrange positive integers in an array such that their product equals a specific target value.

You are given a 2D integer array queries, where each queries[i] = [ni, ki] represents a challenge:

  • ni: The size of the array you need to create
  • ki: The target product that all elements must multiply to

For each query, you need to count how many different ways you can place positive integers into an array of size ni such that the product of all integers equals ki.

Example: If n=2 and k=6, valid arrays include [1,6], [2,3], [3,2], [6,1] - that's 4 different ways!

Since the answer can be extremely large, return each result modulo 109 + 7.

Input & Output

example_1.py — Basic case
$ Input: queries = [[2,6],[3,4],[4,1]]
Output: [4,2,1]
💡 Note: For [2,6]: Arrays like [1,6], [2,3], [3,2], [6,1] give product 6. For [3,4]: Arrays like [1,1,4], [1,2,2] give product 4. For [4,1]: Only [1,1,1,1] gives product 1.
example_2.py — Edge case with 1
$ Input: queries = [[1,1],[2,1]]
Output: [1,1]
💡 Note: When k=1, there's exactly one way: fill all positions with 1. For [1,1]: [1]. For [2,1]: [1,1].
example_3.py — Larger numbers
$ Input: queries = [[3,12],[2,8]]
Output: [18,10]
💡 Note: For [3,12]: 12 = 2² × 3¹, using stars and bars gives C(4,2) × C(3,1) = 6 × 3 = 18 ways. For [2,8]: 8 = 2³, gives C(4,3) = 4 ways for first query type.

Visualization

Tap to expand
Prime Factor Distribution VisualizationExample: Creating arrays of size 3 with product 12Step 1: Factorize12 = 2² × 3¹2² (2 coins)3¹ (1 coin)Two types of coins to distributeStep 2: Stars & Bars3 boxes (array positions)[0][1][2]Distribute coins into boxesStep 3: CalculateCombinatorial Formula2²: C(3+2-1,2) = C(4,2) = 63¹: C(3+1-1,1) = C(3,1) = 3Total: 6 × 3 = 18 waysExample Distributions for 2²:[2,0,0] → [4,1,1][1,1,0] → [2,2,1][1,0,1] → [2,1,2][0,2,0] → [1,4,1][0,1,1] → [1,2,2][0,0,2] → [1,1,4]Then distribute 3¹3 ways each🔑 Key Formula: C(n+exponent-1, exponent)This counts ways to put 'exponent' identical items into 'n' distinct boxes⚡ Time Complexity: O(max_k × log(log(max_k)) + Q × prime_factors(k))Each prime factor contributes independently to the final count!
Understanding the Visualization
1
Prime Factorization
Break k = 12 into 2² × 3¹
2
Distribute Exponent 2
Place two '2' coins into 3 boxes: C(4,2) = 6 ways
3
Distribute Exponent 1
Place one '3' coin into 3 boxes: C(3,1) = 3 ways
4
Multiply Results
Total ways = 6 × 3 = 18
Key Takeaway
🎯 Key Insight: Transform the array product problem into a combinatorial distribution problem using prime factorization and the stars-and-bars formula.

Time & Space Complexity

Time Complexity
⏱️
O(max_k * log(log(max_k)) + Q * d(k))

Sieve for preprocessing + Q queries × number of distinct prime factors

n
2n
Linearithmic
Space Complexity
O(max_k)

Space for storing prime factorizations and precomputed factorials

n
2n
Linear Space

Constraints

  • 1 ≤ queries.length ≤ 104
  • 1 ≤ ni, ki ≤ 104
  • All positive integers must be ≥ 1
  • Answer should be returned modulo 109 + 7
Asked in
Google 42 Meta 28 Amazon 35 Microsoft 19
28.4K Views
Medium Frequency
~35 min Avg. Time
856 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen