|
| 1 | +/** |
| 2 | + * 1735. Count Ways to Make Array With Product |
| 3 | + * https://leetcode.com/problems/count-ways-to-make-array-with-product/ |
| 4 | + * Difficulty: Hard |
| 5 | + * |
| 6 | + * You are given a 2D integer array, queries. For each queries[i], where queries[i] = [ni, ki], |
| 7 | + * find the number of different ways you can place positive integers into an array of size ni |
| 8 | + * such that the product of the integers is ki. As the number of ways may be too large, the |
| 9 | + * answer to the ith query is the number of ways modulo 109 + 7. |
| 10 | + * |
| 11 | + * Return an integer array answer where answer.length == queries.length, and answer[i] is the |
| 12 | + * answer to the ith query. |
| 13 | + */ |
| 14 | + |
| 15 | +/** |
| 16 | + * @param {number[][]} queries |
| 17 | + * @return {number[]} |
| 18 | + */ |
| 19 | +var waysToFillArray = function(queries) { |
| 20 | + const MOD = 1000000007n; |
| 21 | + const MAX_N = 20000; |
| 22 | + const factorial = new Array(MAX_N + 1).fill(1n); |
| 23 | + const inverse = new Array(MAX_N + 1).fill(1n); |
| 24 | + |
| 25 | + for (let i = 1; i <= MAX_N; i++) { |
| 26 | + factorial[i] = (factorial[i - 1] * BigInt(i)) % MOD; |
| 27 | + } |
| 28 | + inverse[MAX_N] = BigInt(modInverse(Number(factorial[MAX_N] % MOD), Number(MOD))); |
| 29 | + for (let i = MAX_N - 1; i >= 0; i--) { |
| 30 | + inverse[i] = (inverse[i + 1] * BigInt(i + 1)) % MOD; |
| 31 | + } |
| 32 | + |
| 33 | + const result = []; |
| 34 | + for (const [size, product] of queries) { |
| 35 | + if (product === 1) { |
| 36 | + result.push(1); |
| 37 | + continue; |
| 38 | + } |
| 39 | + const factors = getPrimeFactors(product); |
| 40 | + let ways = 1n; |
| 41 | + for (const count of factors.values()) { |
| 42 | + const n = size + count - 1; |
| 43 | + ways = (ways * BigInt(combinations(n, count))) % MOD; |
| 44 | + } |
| 45 | + result.push(Number(ways)); |
| 46 | + } |
| 47 | + |
| 48 | + return result; |
| 49 | + |
| 50 | + function modInverse(a, m) { |
| 51 | + const m0 = m; |
| 52 | + let t; |
| 53 | + let q; |
| 54 | + let x0 = 0; |
| 55 | + let x1 = 1; |
| 56 | + while (a > 1) { |
| 57 | + q = Math.floor(a / m); |
| 58 | + t = m; |
| 59 | + m = a % m; |
| 60 | + a = t; |
| 61 | + t = x0; |
| 62 | + x0 = x1 - q * x0; |
| 63 | + x1 = t; |
| 64 | + } |
| 65 | + return x1 < 0 ? x1 + m0 : x1; |
| 66 | + } |
| 67 | + |
| 68 | + function combinations(n, k) { |
| 69 | + if (k < 0 || k > n || n < 0 || n > MAX_N || n - k < 0) return 0; |
| 70 | + const result = (factorial[n] * inverse[k] * inverse[n - k]) % MOD; |
| 71 | + return Number(result); |
| 72 | + } |
| 73 | + |
| 74 | + function getPrimeFactors(num) { |
| 75 | + const factors = new Map(); |
| 76 | + for (let i = 2; i * i <= num; i++) { |
| 77 | + while (num % i === 0) { |
| 78 | + factors.set(i, (factors.get(i) || 0) + 1); |
| 79 | + num /= i; |
| 80 | + } |
| 81 | + } |
| 82 | + if (num > 1) { |
| 83 | + factors.set(num, (factors.get(num) || 0) + 1); |
| 84 | + } |
| 85 | + return factors; |
| 86 | + } |
| 87 | +}; |
0 commit comments