forked from keineahnung2345/leetcode-cpp-practices
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1175. Prime Arrangements.cpp
34 lines (30 loc) · 985 Bytes
/
1175. Prime Arrangements.cpp
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
//Runtime: 4 ms, faster than 54.69% of C++ online submissions for Prime Arrangements.
//Memory Usage: 8.4 MB, less than 100.00% of C++ online submissions for Prime Arrangements.
class Solution {
public:
int modulo = pow(10, 9) + 7;
set<int> primes = {2};
long int factorial(int n){
long int ans = 1;
for(int i = n; i >= 1; i--){
ans *= i;
ans = ans % modulo;
}
return ans;
}
int numPrimeArrangements(int n) {
for(int i = 3; i <= n; i++){
bool isPrime = true;
for(int prime : primes){
if(i%prime == 0){
isPrime = false;
break;
}
}
if(isPrime) primes.insert(i);
}
int pcount = primes.size(), npcount = n - pcount;
// cout << pcount << ", " << npcount << endl;
return (factorial(pcount) * factorial(npcount)) % modulo;
}
};