-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem-1175.cpp
More file actions
44 lines (36 loc) · 918 Bytes
/
Problem-1175.cpp
File metadata and controls
44 lines (36 loc) · 918 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//Problem - 1175
// https://leetcode.com/problems/prime-arrangements/
// O(n) time comlexity
class Solution {
public:
int mod = 1e9 + 7;
bool isPrime(int n) {
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
long long fact(int n) {
long long fac = 1;
int x = 2;
while(x <= n) {
fac = (fac*x)%mod;
x++;
}
return fac;
}
int numPrimeArrangements(int n) {
int primes = 0, nonprimes = 0;
for(int i = 1; i <= n; i++)
if(isPrime(i))
primes++;
nonprimes = n - primes;
return (fact(primes) * fact(nonprimes) )%mod;
}
};