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.
Constraints
- 1 ≤ queries.length ≤ 104
- 1 ≤ ni, ki ≤ 104
- All positive integers must be ≥ 1
- Answer should be returned modulo 109 + 7
Visualization
Tap to expand
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.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code