-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathThe Number of Good Subsets.java
123 lines (100 loc) · 3.73 KB
/
The Number of Good Subsets.java
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Runtime: 4 ms (Top 100.0%) | Memory: 63.20 MB (Top 13.04%)
class Solution {
static int MOD = 1_000_000_000 + 7;
// These numbers contain duplicate factors (e.g 4, 8, 9, 25), will be excluded
static List<Integer> excludes = new ArrayList<>();
// Distinct prime factors of composites
// e.g 6 = 2 * 3, 15 = 3 * 5, 30 = 2 * 3 * 5
static List<Integer>[] factors = new List[31];
// Coprime numbers permutation
// Coprime means some composites don't have common factor and can coexist
// e.g. 14 = 2 * 7 and 15 = 3 * 5
static List<int[]> coprimes_pmt = new ArrayList<>();
static {
List<Integer> primes = Arrays.asList(2, 3, 5, 7, 11, 13, 17, 19, 23, 29);
int[] masks = new int[31];
for (int i = 4; i <= 30; i++) {
// exclude 4, 8, 9, 25 ...
if (i % 4 == 0 || i % 9 == 0 || i % 25 == 0) {
excludes.add(i);
continue;
}
if (primes.contains(i)) {
continue;
}
// Set distinct prime factors of composites
for (int j = 0; j < primes.size(); j++) {
if (i % primes.get(j) == 0) {
if (factors[i] == null) {
factors[i] = new ArrayList<>();
}
factors[i].add(primes.get(j));
masks[i] |= (1 << j);
}
}
}
// Recursively build coprime permutation
buildCoprimes(0, masks, 0, new int[]{});
}
static void buildCoprimes(int mask, int[] masks, int num, int[] prev) {
for (; num < masks.length; num++) {
if (masks[num] > 0 && (mask & masks[num]) == 0) {
int[] arr = Arrays.copyOf(prev, prev.length + 1);
arr[prev.length] = num;
coprimes_pmt.add(arr);
buildCoprimes(mask | masks[num], masks, num + 1, arr);
}
}
}
public int numberOfGoodSubsets(int[] nums) {
int[] prime_count = new int[31];
int[] composite_count = new int[31];
for (int num : nums) {
prime_count[num]++;
}
// exclude numbers having duplicate factors, like 4, 8, 9, 25...
for (int ex : excludes) {
prime_count[ex] = 0;
}
// split prime numbers and composite numbers
for (int i = 0; i < prime_count.length; i++) {
if (factors[i] != null) {
composite_count[i] = prime_count[i];
prime_count[i] = 0;
}
}
// sum result for prime numbers
long result = sum(prime_count, null);
// sum result for coprime numbers
for (int[] coprimes : coprimes_pmt) {
long count_mul = 1;
for (int composite : coprimes) {
count_mul *= composite_count[composite];
}
if (count_mul > 0) {
result = (result + (sum(prime_count, coprimes) + 1) * count_mul) % MOD;
}
}
// Each `1` will double the result
while (prime_count[1] > 0) {
result = (result * 2) % MOD;
prime_count[1]--;
}
return (int) result;
}
int sum(int[] prime_count, int[] coprimes) {
int[] dp = Arrays.copyOf(prime_count, prime_count.length);
// Exclude prime factors of coprime numbers
if (coprimes != null) {
for (int composite : coprimes) {
for (int factor : factors[composite]) {
dp[factor] = 0;
}
}
}
for (int i = 3; i <= 29 ; i++) {
dp[i] = (int) ((dp[i - 1] + 1L * dp[i - 1] * dp[i] + dp[i]) % MOD);
}
return dp[29];
}
}