AcWing 890. 能被整除的数
来自大佬的博文
ttps://img-blog.csdnimg.cn/20210221211724332.png)
这是一道典型的容斥问题,凡是个数为奇数的就系数为正,然后偶数为负
y总的视频讲的很清楚首先我们想办法把n个数的的倍数求出来求出他们的个数,然后进行一个容斥运算,为什么要用二进制呢,因为1到2m-1是0位到m-1位的0,1的全排列。所以我们引用这部分的知识
#include<iostream>
using namespace std;
const int N=20;
typedef long long LL;
int p[N];
int main(void)
{
int n,m,res=0;
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++) cin>>p[i];
for(int i=1;i<1<<m;i++)
{
int t=1,cnt=0;
for(int j=0;j<m;j++)
{
if(i>>j&1)
{
cnt++;
if((LL)t*p[j]>n)
{
t=0;
break;
}
t*=p[j];
}
}
if(t)
{
if(cnt&1) res+=n/t;
else
res-=n/t;
// cout<<res<<endl;
}
}
cout<<res;
}