hdu1796 How many integers can you find(容斥)

题目链接:点这里!!!!


题意:给你一个n(n<=2^31),m(m<=10),再给你m个数a[i](0<=a[i]<=20),问你[1,n-1]范围里有多少个数能被这m个数中的一个或多个整除。


题解:容斥裸体,注意a[i]=0的情况。

sum = 被1个数整除的个数-被2个数整除的个数+被3个数整除的个数-.... (注意被多个数整除的时候,我们利用的是lcm)


代码:


#include<cstdio>
#include<cstring>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<vector>
#include<bitset>
#include<set>
#include<queue>
#include<stack>
#include<map>
#include<cstdlib>
#include<cmath>
#define PI 2*asin(1.0)
#define LL long long
#define pb push_back
#define pa pair<int,int>
#define clr(a,b) memset(a,b,sizeof(a))
#define lson lr<<1,l,mid
#define rson lr<<1|1,mid+1,r
#define bug(x) printf("%d++++++++++++++++++++%d\n",x,x)
#define key_value ch[ch[root][1]][0]C:\Program Files\Git\bin
const LL  MOD = 1E9+7;
const LL N = 2e5+15;
const int maxn = 5e5+15;
const int letter = 130;
const int INF = 1e17;
const double pi=acos(-1.0);
const double eps=1e-10;
using namespace std;
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int n,m,a[15];
LL gcd(LL a,LL b){
    if(b==0) return a;
    return gcd(b,a%b);
}
LL lcm(LL a,LL b){
    return a*b/gcd(a,b);
}
int main(){
    while(scanf("%d%d",&n,&m)!=EOF){
        for(int i=0;i<m;i++) {
            scanf("%d",a+i);
            if(!a[i])i--,m--;
        }
        n--;
        LL sum=0,ans;
        int num;
        for(int i=1;i<(1<<m);i++){
            ans=1;
            num=0;
            for(int j=0;j<m;j++){
                if((1<<j)&i){
                    num++;
                    ans=lcm(ans,1ll*a[j]);
                }
            }
            if(num%2) sum+=1ll*n/ans;
            else sum-=1ll*n/ans;
        }
        printf("%I64d\n",sum);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值