Skip to content

Commit 3780703

Browse files
authored
Create 1994.The-Number-of-Good-Subsets_v1.cpp
1 parent 45337c6 commit 3780703

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
class Solution {
2+
long M = 1e9+7;
3+
public:
4+
int numberOfGoodSubsets(vector<int>& nums)
5+
{
6+
unordered_set<int>Set({2,3,5,6,7,10,11,13,14,15,17,19,21,22,23,26,29,30});
7+
map<int,int>Map;
8+
int count1 = 0;
9+
for (int x: nums)
10+
{
11+
if (Set.find(x)!=Set.end())
12+
Map[x]+=1;
13+
if (x==1)
14+
count1++;
15+
}
16+
17+
int n = Map.size();
18+
vector<int>count;
19+
vector<int>digit;
20+
for (auto p: Map)
21+
{
22+
digit.push_back(p.first);
23+
count.push_back(p.second);
24+
}
25+
26+
long ret = 0;
27+
for (int state=1; state<(1<<n); state++)
28+
{
29+
int flag = 1;
30+
for (int i=0; i<n; i++)
31+
{
32+
if (((state>>i)&1)==0) continue;
33+
for (int j=i+1; j<n; j++)
34+
{
35+
if (((state>>j)&1)==0) continue;
36+
if (gcd(digit[i], digit[j])!=1)
37+
{
38+
flag = 0;
39+
break;
40+
}
41+
}
42+
if (flag==0)
43+
break;
44+
}
45+
46+
if (flag==0) continue;
47+
48+
long ans = 1;
49+
for (int i=0; i<n; i++)
50+
{
51+
if (((state>>i)&1)==0) continue;
52+
ans *= count[i];
53+
ans %= M;
54+
}
55+
ret = (ret+ans) % M;
56+
}
57+
58+
ret = ret * quickMul(2, count1) % M;
59+
return ret;
60+
}
61+
62+
long quickMul(long x, long N) {
63+
if (N == 0) {
64+
return 1;
65+
}
66+
long y = quickMul(x, N / 2);
67+
return N % 2 == 0 ? y * y % M: y * y % M * x % M;
68+
}
69+
};

0 commit comments

Comments
 (0)