forked from sameer2800/Lightoj-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1117-helping-cicada.cpp
91 lines (63 loc) · 1.05 KB
/
1117-helping-cicada.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
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
#include <bits/stdc++.h>
#define ll long long
#define MAX 100100
using namespace std;
ll arr[16];
ll dp[1 << 16];
ll ans = 0;
ll n;
ll lcm(ll a,ll b)
{
ll yi = __gcd(a,b);
return (a*b)/yi;
}
void recursive(int curr,int curbit, int mbits)
{
if(curbit >= mbits){
return;
}
if(dp[curr] != -1)
return;
int yo = __builtin_popcount(curr);
ll temp =1;
for(int i=0;i < mbits;i++){
if(curr & 1 << i ){
temp = lcm(arr[i],temp);
}
}
if(curr != 0) {
if(yo % 2 == 1) {
ans += n/temp;
}
else {
ans -= n/temp;
}
}
for(int i =0;i < mbits;i++){
if(!(curr & 1 << i )) {
recursive(curr | 1 << i ,i,mbits);
}
}
dp[curr] = 1;
return;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("/home/sameer/sameer/sam.sam","r",stdin);
#endif
ios_base::sync_with_stdio(false);
ll i,j,k,m,t,cont;
cin >> t;
ll cases = t;
while(t--){
memset(dp,-1,sizeof dp);
ans = 0;
cin >> n >> m;
for(i =0;i < m;i++)
cin >> arr[i];
recursive(0,0,m);
cout << "Case " << cases - t << ": " << n- ans << endl;
}
return 0;
}