-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame Of Subsets
85 lines (75 loc) · 1.87 KB
/
Game Of Subsets
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
class Solution{
unordered_map<int,int>cap;
int mod = (int)(1e9+7);
bool check(int n) {
int c = 2;
while (n > 1) {
if (n % c == 0) {
n/= c;
if (n % c == 0) return false;
} else {
c++;
}
}
true;
}
int fun(int i, vector<int>temp, vector<int>&arr) {
if (i < 0) return 0;
long long not_pick = fun(i-1, temp, arr);
long long pick = 0;
bool flag = true;
for (auto &x: temp) {
if (arr[i] % x == 0) {
flag = false;
break;
}
}
if (flag == true) {
int val = arr[i];
int c = 2;
int m = 0;
vector<int>temp2;
bool flag2 = true;
while (val > 1) {
if (val % c == 0) {
val = val / c;
if (val % c == 0) {
flag2 = false;
break;
}
temp2.push_back(c);
} else {
c++;
}
}
if (flag2 == true) {
for (auto &x : temp2) {
temp.push_back(x);
}
pick = (1 + fun(i-1 , temp, arr));
}
}
// cout<<arr[i]<<" "<<pick<<" "<<not_pick<<endl;
long long val2 = cap[arr[i]];
return (val2*pick + not_pick)%mod;
}
public:
int goodSubsets(vector<int> &arr, int n){
int ones = 0;
vector<int>nums;
for (auto &x : arr) {
if (x == 1) ones++;
else {
if (cap[x] == 0 && check(x)) {
nums.push_back(x);
//cout<<x<<endl;
}
cap[x]++;
}
}
vector<int>s;
int ans = fun(nums.size()-1, s, nums);
cap.clear();
return ans*pow(2, ones);
}
};