集训Day1 T3 整除

来源:互联网 发布:软件编程 找工作多少钱 编辑:程序博客网 时间:2024/04/30 00:49

Description

给出n个数a1,a2……an,求区间[L,R]中有多少个整数不能被其中任何一个数整除。

Input

第一行三个正整数,n,L,R。

第二行n个正整数a1,a2……an

Output

一个数,即区间[L,R]中有多少个整数不能被其中任何一个数整除。

Sample Input

2 1 1000

10 15

Sample Output

867

Data Constraint
对于30%的数据,1<=n<=10,1<=L,R<=1000

对于100%的数据,1<=n<=18,1<=L,R<=10^9

思路:容斥 ,由于n比较小,可以直接暴力dfs,看看每次选了几个数,奇数加偶数减统计答案
代码如下:

#include <cstdio>#include <cstring>#include <iostream>using namespace std;long long l,r,ans,a[21];int n;long long gcd(long long a,long long b){    if (b==0)   return a;    gcd(b,a % b);}long long lcm(long long a,long long b){    return (a*b)/gcd(a,b);}void dfs(int dep,int s,long long zs){    if (dep>n)    {        if (s%2==1) ans+=r/zs-(l-1)/zs;        else ans-=r/zs-(l-1)/zs;        return;    }    dfs(dep+1,s+1,lcm(a[dep],zs));    dfs(dep+1,s,zs);}int main(){    //freopen("number.in","r",stdin);    //freopen("number.out","w",stdout);    scanf("%d%lld%lld",&n,&l,&r);    for (int i=1;i<=n;i++)        scanf("%lld",&a[i]);    dfs(1,1,1);    printf("%lld",ans);}
原创粉丝点击