{"id":5511,"date":"2019-08-31T22:10:02","date_gmt":"2019-09-01T05:10:02","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5511"},"modified":"2019-09-01T17:59:31","modified_gmt":"2019-09-02T00:59:31","slug":"leetcode-1175-prime-arrangements","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-1175-prime-arrangements\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1175. Prime Arrangements"},"content":{"rendered":"\n<p>Return the number of permutations of 1 to&nbsp;<code>n<\/code>&nbsp;so that prime numbers are at prime indices (1-indexed.)<\/p>\n\n\n\n<p><em>(Recall that an integer&nbsp;is prime if and only if it is greater than 1, and cannot be written as a product of two positive integers&nbsp;both smaller than it.)<\/em><\/p>\n\n\n\n<p>Since the answer may be large, return the answer&nbsp;<strong>modulo&nbsp;<code>10^9 + 7<\/code><\/strong>.<\/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> n = 5\n<strong>Output:<\/strong> 12\n<strong>Explanation:<\/strong> For example [1,2,5,4,3] is a valid permutation, but [5,2,3,4,1] is not because the prime number 5 is at index 1.\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> n = 100\n<strong>Output:<\/strong> 682289015\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= n &lt;= 100<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Permutation<\/strong><\/h2>\n\n\n\n<p>Count the number of primes in range [1, n], assuming there are p primes and n &#8211; p non-primes, we can permute each group separately. <br>ans = p!  * (n &#8211; p)!<\/p>\n\n\n\n<p>Time complexity: O(nsqrt(n))<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 numPrimeArrangements(int n) {\n    const int kMod = 1e9 + 7;\n    int p = 0;\n    for (int i = 1; i <= n; ++i)\n      p += isPrime(i);\n    long ans = 1;\n    for (int i = 1; i <= p; ++i)\n      ans = (ans * i) % kMod;\n    for (int i = 1; i <= n - p; ++i)\n      ans = (ans * i) % kMod;\n    return ans;\n  }\nprivate:\n  bool isPrime(int x) {\n    if (x < 2) return false;\n    if (x == 2) return true;\n    for (int i = 2; i <= sqrt(x); ++i)\n      if (x % i == 0) return false;\n    return true;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Return the number of permutations of 1 to&nbsp;n&nbsp;so that prime numbers are at prime indices (1-indexed.) (Recall that an integer&nbsp;is prime if and only if&#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":[222,121,59],"class_list":["post-5511","post","type-post","status-publish","format-standard","hentry","category-math","tag-easy","tag-permutation","tag-prime","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5511","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=5511"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5511\/revisions"}],"predecessor-version":[{"id":5520,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5511\/revisions\/5520"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5511"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5511"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5511"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}