{"id":8290,"date":"2021-03-27T22:49:16","date_gmt":"2021-03-28T05:49:16","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8290"},"modified":"2021-03-28T16:29:11","modified_gmt":"2021-03-28T23:29:11","slug":"leetcode-1808-maximize-number-of-nice-divisors","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-1808-maximize-number-of-nice-divisors\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1808. Maximize Number of Nice Divisors"},"content":{"rendered":"\n<p>You are given a positive integer&nbsp;<code>primeFactors<\/code>. You are asked to construct a positive integer&nbsp;<code>n<\/code>&nbsp;that satisfies the following conditions:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The number of prime factors of&nbsp;<code>n<\/code>&nbsp;(not necessarily distinct) is&nbsp;<strong>at most<\/strong>&nbsp;<code>primeFactors<\/code>.<\/li><li>The number of nice divisors of&nbsp;<code>n<\/code>&nbsp;is maximized. Note that a divisor of&nbsp;<code>n<\/code>&nbsp;is&nbsp;<strong>nice<\/strong>&nbsp;if it is divisible by every prime factor of&nbsp;<code>n<\/code>. For example, if&nbsp;<code>n = 12<\/code>, then its prime factors are&nbsp;<code>[2,2,3]<\/code>, then&nbsp;<code>6<\/code>&nbsp;and&nbsp;<code>12<\/code>&nbsp;are nice divisors, while&nbsp;<code>3<\/code>&nbsp;and&nbsp;<code>4<\/code>&nbsp;are not.<\/li><\/ul>\n\n\n\n<p>Return&nbsp;<em>the number of nice divisors of<\/em>&nbsp;<code>n<\/code>. Since that number can be too large, return it&nbsp;<strong>modulo<\/strong>&nbsp;<code>10<sup>9<\/sup>&nbsp;+ 7<\/code>.<\/p>\n\n\n\n<p>Note that a prime number is a natural number greater than&nbsp;<code>1<\/code>&nbsp;that is not a product of two smaller natural numbers. The prime factors of a number&nbsp;<code>n<\/code>&nbsp;is a list of prime numbers such that their product equals&nbsp;<code>n<\/code>.<\/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> primeFactors = 5\n<strong>Output:<\/strong> 6\n<strong>Explanation:<\/strong> 200 is a valid value of n.\nIt has 5 prime factors: [2,2,2,5,5], and it has 6 nice divisors: [10,20,40,50,100,200].\nThere is not other value of n that has at most 5 prime factors and more nice divisors.\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> primeFactors = 8\n<strong>Output:<\/strong> 18\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= primeFactors &lt;= 10<sup>9<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Math<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(logn)<br>Space complexity: O(1)<\/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  int maxNiceDivisors(int n) {\n    constexpr int kMod = 1e9 + 7;\n    auto powm = [](long base, int exp) {\n      long ans = 1;\n      while (exp) {\n        if (exp & 1) ans = (ans * base) % kMod;\n        base = (base * base) % kMod;\n        exp >>= 1;\n      }\n      return ans;\n    };\n    \n    if (n <= 3) return n;\n    switch (n % 3) {\n      case 0: return powm(3, n \/ 3);\n      case 1: return (powm(3, n \/ 3 - 1) * 4) % kMod;\n      case 2: return (powm(3, n \/ 3) * 2) % kMod;\n    }\n    return -1;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a positive integer&nbsp;primeFactors. You are asked to construct a positive integer&nbsp;n&nbsp;that satisfies the following conditions: The number of prime factors of&nbsp;n&nbsp;(not necessarily&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[701,217,31,59],"class_list":["post-8290","post","type-post","status-publish","format-standard","hentry","category-math","tag-factors","tag-hard","tag-math","tag-prime","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8290","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=8290"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8290\/revisions"}],"predecessor-version":[{"id":8292,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8290\/revisions\/8292"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8290"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8290"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8290"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}