0
点赞
收藏
分享

微信扫一扫

容斥原理 能被整除的数

认真的老去 2022-03-12 阅读 85

 

 

n中p的倍数元素的个数 n/p下取整

如果是2 和 3 的倍数 就是6的倍数 n/6下取整

 

#include<iostream>
using namespace std;

typedef long long LL;
const int N=20;
int p[N],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++)
    {
        int t=1,cnt=0;
        
        for(int j=0;j<m;j++)
        {
            if(i >> j & 1)
            {
                cnt++;
                if((LL)t * p[j] > n)
                {
                    t=-1;
                    break;
                }
                t *= p[j];
            }
        }
        
        if(t != -1)
        {
            if(cnt % 2) res += n/ t;
            else res -=n / t;
        }
        
        
    }
    
    cout<<res<<endl;
    return 0;
}
举报

相关推荐

0 条评论