{"id":8017,"date":"2021-01-23T21:29:33","date_gmt":"2021-01-24T05:29:33","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8017"},"modified":"2021-01-23T21:29:51","modified_gmt":"2021-01-24T05:29:51","slug":"leetcode-1735-count-ways-to-make-array-with-product","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-1735-count-ways-to-make-array-with-product\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1735. Count Ways to Make Array With Product"},"content":{"rendered":"\n<p>You are given a 2D integer array,&nbsp;<code>queries<\/code>. For each&nbsp;<code>queries[i]<\/code>, where&nbsp;<code>queries[i] = [n<sub>i<\/sub>, k<sub>i<\/sub>]<\/code>, find the number of different ways you can place positive integers into an array of size&nbsp;<code>n<sub>i<\/sub><\/code>&nbsp;such that the product of the integers is&nbsp;<code>k<sub>i<\/sub><\/code>. As the number of ways may be too large, the answer to the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;query is the number of ways&nbsp;<strong>modulo<\/strong>&nbsp;<code>10<sup>9<\/sup>&nbsp;+ 7<\/code>.<\/p>\n\n\n\n<p>Return&nbsp;<em>an integer array&nbsp;<\/em><code>answer<\/code><em>&nbsp;where&nbsp;<\/em><code>answer.length == queries.length<\/code><em>, and&nbsp;<\/em><code>answer[i]<\/code><em>&nbsp;is the answer to the&nbsp;<\/em><code>i<sup>th<\/sup><\/code><em>&nbsp;query.<\/em><\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> queries = [[2,6],[5,1],[73,660]]\n<strong>Output:<\/strong> [4,1,50734910]\n<strong>Explanation:<\/strong>&nbsp;Each query is independent.\n[2,6]: There are 4 ways to fill an array of size 2 that multiply to 6: [1,6], [2,3], [3,2], [6,1].\n[5,1]: There is 1 way to fill an array of size 5 that multiply to 1: [1,1,1,1,1].\n[73,660]: There are 1050734917 ways to fill an array of size 73 that multiply to 660. 1050734917 modulo 10<sup>9<\/sup> + 7 = 50734910.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> queries = [[1,1],[2,2],[3,3],[4,4],[5,5]]\n<strong>Output:<\/strong> [1,2,3,10,5]\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= queries.length &lt;= 10<sup>4<\/sup><\/code><\/li><li><code>1 &lt;= n<sub>i<\/sub>, k<sub>i<\/sub>&nbsp;&lt;= 10<sup>4<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution1: DP<\/strong><\/h2>\n\n\n\n<p>let dp(n, k) be the ways to have product k of array size n.<br>dp(n, k) = sum(dp(n &#8211; 1, i)) where i is a factor of k and i != k.<br>base case:<br>dp(0, 1) = 1, dp(0, *) = 0<br>dp(i, 1) = C(n, i)<br>e.g. <br>dp(2, 6) = dp(1, 1) + dp(1, 2) + dp(1, 3) <br>= 2 + 1 + 1 = 4<br>dp(4, 4) = dp(3, 1) + dp(3, 2) <br>= dp(3, 1) + dp(2, 1)<br>= 4 + 6 = 10<br><\/p>\n\n\n\n<p>Time complexity: O(sum(k_i))?<br>Space complexity: O(sum(k_i))?<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<int> waysToFillArray(vector<vector<int>>& queries) {\n    constexpr int kMod = 1e9 + 7;\n    int n = 0;\n    unordered_map<int, int> c, dp;\n    function<int(int, int)> cnk = [&](int n, int k) {\n      if (k > n) return 0;\n      if (k == 0 || k == n) return 1;      \n      int& ans = c[(n << 16) | k];\n      if (!ans) ans = (cnk(n - 1, k - 1) + cnk(n - 1, k)) % kMod; \n      return ans;      \n    };\n    \n    function<int(int, int)> dfs = [&](int s, int k) -> int {\n      if (s == 0) return k == 1;\n      if (k == 1) return cnk(n, s);\n      int& ans = dp[(s << 16) | k];\n      if (ans) return ans;      \n      for (int i = 1; i * i <= k; ++i) {\n        if (k % i) continue;\n        if (i != 1) \n          ans = (ans + dfs(s - 1, k \/ i)) % kMod;\n        if (i * i != k)\n          ans = (ans + dfs(s - 1, i)) % kMod;\n      }\n      return ans;\n    };\n    \n    vector<int> ans;\n    for (const auto& q : queries) {\n      dp.clear();\n      n = q[0];\n      ans.push_back(dfs(n, q[1]));\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a 2D integer array,&nbsp;queries. For each&nbsp;queries[i], where&nbsp;queries[i] = [ni, ki], find the number of different ways you can place positive integers into&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[18,441,217,59],"class_list":["post-8017","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-factor","tag-hard","tag-prime","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8017","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=8017"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8017\/revisions"}],"predecessor-version":[{"id":8019,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8017\/revisions\/8019"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8017"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8017"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8017"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}