HDU 1796 容斥原理

思路:

就二进制枚举哪些数,然后取最小公倍数

搞一搞

//By SiriusRen
#include <cstdio>
using namespace std;
#define int long long
int n,r,m,s[15];
int gcd(int a,int b){return b?gcd(b,a%b):a;}
signed main(){
    while(~scanf("%lld%lld",&n,&r)){
        int ans=0;n--;m=0;
        for(int i=0;i<r;i++){
            scanf("%lld",&s[m]);
            if(s[m])m++;
        }
        for(int i=1;i<(1<<m);i++){
            int tmp=1,t=0;
            for(int j=0;j<m;j++){
                if(i&(1<<j)){
                    t++,tmp=tmp*s[j]/gcd(tmp,s[j]);
                }
            }
            t=t&1?1:-1;
            ans+=t*n/tmp;
        }
        printf("%lld\n",ans);
    }
}

 

posted @ 2018-08-01 15:42  SiriusRen  阅读(109)  评论(0编辑  收藏  举报