Leetcode-5173 Prime Arrangements(质数排列)

 1 #define _for(i,a,b) for(int i = (a);i < b;i ++)
 2 #define _rep(i,a,b) for(int i = (a);i > b;i --)
 3 
 4 class Solution
 5 {
 6     public:
 7         bool is_prime(int n)
 8         {
 9             for(int i = 2; i * i <= n; i ++)
10                 if(n%i==0) return false;
11             return n != 1;
12         }
13         long long go(int k)
14         {
15             long long rnt = 1;
16             _for(i,1,k+1)
17             {
18                 rnt = i*rnt;
19                 rnt %= 1000000007;
20             }
21             return (int)rnt;
22         }
23         int numPrimeArrangements(int n)
24         {
25             int k = 0;
26             _for(i,1,n+1)
27             if(is_prime(i))
28                 k ++;
29             return (int)((go(k)*go(n-k))%1000000007);
30         }
31 };

 

posted @ 2019-09-01 13:05  Asurudo  阅读(210)  评论(0编辑  收藏  举报