能被整除的数
给定一个整数 n 和 m 个不同的质数 p1,p2,…,pm。
请你求出 1∼n 中能被 p1,p2,…,pm 中的至少一个数整除的整数有多少个。
输入格式
第一行包含整数 n 和 m。
第二行包含 m 个质数。
输出格式
输出一个整数,表示满足条件的整数的个数。
数据范围
1≤m≤16,
1≤n,pi≤109
输入样例:
10 2
2 3
输出样例:
7
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
int n,m;
int a[20];
int main()
{
ios::sync_with_stdio(false);
cin>>n>>m;
for (int i=0;i<m;i++) cin>>a[i];
int res = 0;
for (int i=1;i<1<<m;i++)
{
int t=1,s=0;
for (int j=0;j<m;j++)
{
if (i>>j&1) {
if ((LL)t*a[j]>n)
{
t = -1;
break;
}
t*=a[j];
s++;
}
}
if (t!=-1)
{
if (s%2) res+=n/t;
else
res-=n/t;
}
}
printf("%d",res);
return 0;
}