875. Product Arrays

A label printer must produce codes made of n positive integers whose product is exactly k. You are given queries, where queries[i] = [n, k] asks: how many different arrays of length n consisting of positive integers have a product equal to k? Arrays with the same numbers in a different order count as different arrays, and the value 1 may be used any number of times.

Return an array of the same length as queries where entry i is the answer to query i modulo 10^9 + 7.

Example 1

Input:
queries = [[2,12],[3,8]]
Output:
[6,10]
Explanation:

Length 2 with product 12: (1,12), (2,6), (3,4) and their reversals give 6 arrays. Length 3 with product 8 = 2^3: C(5,3) = 10 arrays.

Example 2

Input:
queries = [[4,30],[1,97],[5,1]]
Output:
[64,1,1]
Explanation:

30 = 2*3*5: each of the three primes goes to one of 4 positions, 4^3 = 64. A single position must hold 97, and five 1s are the only array with product 1.

Example 3

Input:
queries = [[6,64]]
Output:
[462]
Explanation:

64 = 2^6 spread over 6 positions: C(6 + 6 - 1, 6) = 462.

Constraints

  • 1 ≤ queries.length ≤ 104
  • 1 ≤ n ≤ 105 and 1 ≤ k ≤ 106 in every query

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Time per case
Python 1,200 msC++ 300 msJava 600 msJavaScript 600 msTypeScript 600 ms

Expected complexity

Time
O(max n + q * sqrt(k) / log k)
Space
O(max n)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…