Given an integer n and m different prime numbers p1,p2,...,pm.
Please find out how many integers from 1∼n are divisible by at least one of p1,p2,…,pm.
input format
The first line contains the integers n and m.
The second row contains m prime numbers.
output format
Output an integer representing the number of integers that satisfy the condition.
data range
1≤m≤16,
1≤n,pi≤10^9,
Input sample:
10 2
2 3
Sample output:
7
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
const int N = 20;
int p[N];
int n,m;
int main()
{
cin>>n>>m;
for(int i=0;i<m;i++) cin>>p[i];
int res = 0;
for(int i=1;i<(1<<m);i++){ //(1>>m)相当于2^m
int t = 1,s = 0; //t表示当前选取的集合的乘积,s表示已经选取的集合
for(int j=0;j<m;j++){ //j<m表示的是最大被选取的集合数为m
if(i>>j&1){ //例如5:101,表示集合1和集合3已被选取
if((LL)t*p[j]>n){
t = -1;
break;
}
t*=p[j];//更新集合的乘积
s++; //更新被选取的集合
}
}
if(t!=-1){//韦恩图的奇加偶减的规律
if(s%2) res+=(n/t);
else res-=(n/t);
}
}
cout<<res<<endl;
return 0;
}