3476. 【NOIP2013初赛】整除

来源:互联网 发布:桌面股票小软件 编辑:程序博客网 时间:2024/06/10 19:04

Description


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

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

Output


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

Data Constraint


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

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

Solution


任意一对L, R, k单独对答案的贡献为kRkL1,然后我们发现某些数字的lcm会重复算,那么大力容斥一下就可以啦

Code


#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int n,L,R,S,P,v[20];inline int gcd(int a,int b){    for(int c;b;a=b,b=c) c=a%b;    return a;}void dfs(int x,long long s,int d){    if(s>S) return;    if(x==n){        P+=d*(S/s);        return;    }    dfs(x+1,s,d);    dfs(x+1,s/gcd(s,v[x])*v[x],-d);}int gAns(int K){ S=K; P=0; dfs(0,1,1); return P; }int main(){     scanf("%d%d%d",&n,&L,&R);     for(int i=0;i<n;++i) scanf("%d",v+i);    printf("%d\n",gAns(R)-gAns(L-1));}
原创粉丝点击