UVA10325 The Lottery

https://www.luogu.com.cn/problem/UVA10325

题意:

给出m个数,求n以内不能被任意一个数整除的个数

容斥原理

水题洗刷刷

#include<cstdio>
#include<algorithm>

using namespace std;

int n,m,ans;

int a[17];

long long lcm(long long x,long long y)
{
    return x*y/__gcd(x,y);
}

void dfs(int x,int y,long long k)
{
    if(k>n) return;
    if(x==m+1)
    {
        if(y&1) ans-=n/k;
        else ans+=n/k;
        return;
    }
    dfs(x+1,y,k);
    dfs(x+1,y+1,lcm(k,a[x]));
}

int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=1;i<=m;++i) scanf("%d",&a[i]);
        ans=0; 
        dfs(1,0,1);
        printf("%d
",ans);
    } 
}
原文地址:https://www.cnblogs.com/TheRoadToTheGold/p/13761460.html