How many integers can you find HDU - 1796_容斥计数

题目过于智障,不用解释

#include<cstdio>
using namespace std;
typedef long long ll;
const int R=13;
ll a[R];
ll n,ans;
int m,cnt=0;
ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
void dfs(int cur,ll lcm,int id){
    if(cur>cnt)return;
    lcm=a[cur]/gcd(a[cur],lcm)*lcm;

    if(id)ans+=(n-1)/lcm;
    else ans-=(n-1)/lcm;
    for(int i=cur+1;i<=cnt;++i)
        dfs(i,lcm,!id);
}
int main(){
    while(scanf("%lld%d",&n,&m)!=EOF)
    {
        ans=cnt=0;
        for(int i=1;i<=m;++i){
            ll k;
            scanf("%lld",&k);
            if(k)a[++cnt]=k;
        }
        for(int i=1;i<=cnt;++i)
            dfs(i,a[i],1);
        printf("%lld\n",ans);
    }
    return 0;
}

  

posted @ 2018-09-26 12:29  EM-LGH  阅读(119)  评论(0编辑  收藏  举报