HDU - 1796 How many integers can you find (容斥原理)

题意:求[1,N-1]中有多少能被集合M中的任意一个数整除。

分析:运用容斥原理解决。二进制枚举N-1中能被1个,2个...M个数整除的个数,奇加偶减。

每次N-1除以若干个数的lcm得到区间[1,N-1]中能被这若干个数整除的个数。

注意M中有可能出现0,0是不能整除任何数的,跳过即可。

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;

LL gcd(LL a,LL b){
    if(b==0) return a;
    return gcd(b,a%b);
}

LL lcm(LL a,LL b){
    return a/gcd(a,b)*b;
}

LL vz[20];

int main()
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif  
    LL N,M;
    while(scanf("%lld %lld",&N,&M)==2){
        N--;
        int tot=0;
        for(int i=0;i<M;++i){
            LL tmp; scanf("%lld",&tmp);
            if(!tmp) continue;              //0无效
            vz[tot++] = tmp;
        }
        LL ans = 0;
        M = tot;
        for(int i =1;i<(1<<M);++i){
            LL cnt = 0, lc = 1;
            for(int j=0; j<M ;++j){
                if(i&(1<<j)){
                    cnt++;
                    lc = lcm(lc,vz[j]);
                }
            }
            if(cnt&1) ans +=N/lc;
            else ans -=N/lc;
        }
        printf("%lld\n",ans);
    }
    return 0;
}

 

posted @ 2018-08-20 10:28  xiuwenL  阅读(118)  评论(0编辑  收藏  举报