-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathLC1175.cpp
More file actions
executable file
·39 lines (34 loc) · 829 Bytes
/
LC1175.cpp
File metadata and controls
executable file
·39 lines (34 loc) · 829 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
/*
Problem Statement: https://leetcode.com/problems/prime-arrangements/
Time: O(n • sqrt(n))
Space: O(n)
Author: Mohammed Shoaib, github.com/Mohammed-Shoaib
*/
class Solution {
private:
int count_primes(int n) {
vector<int> prime(n + 1, true);
// sieve of eratosthenes
prime[0] = prime[1] = false;
for (int i = 2; i * i <= n; i++) {
if (!prime[i])
continue;
for (int j = 2 * i; j <= n; j += i)
prime[j] = false;
}
return count(prime.begin(), prime.end(), true);
}
int factorial(int64_t x, int mod) {
if (x == 0)
return 1;
for (int y = x - 1; y > 1; y--)
x = (x * y) % mod;
return x;
}
public:
int numPrimeArrangements(int n) {
int primes = count_primes(n), mod = 1e9 + 7;
int64_t a = factorial(primes, mod), b = factorial(n - primes, mod);
return (a * b) % mod;
}
};