ZOJ

来源:互联网 发布:黑马程序员薪资 编辑:程序博客网 时间:2024/06/02 06:39
题目:给你2个数组x,y,求low~high这个范围内,满足至少能被x中任意一个数整除并且至少不能被y中任意一个数整除。
思路:直接求至少不能被y中任意一个数整除的个数不好求,我们可以求整除y中所有数的个数,用容斥定理在算的时候顺便减去非法的就好了

代码:

#pragma comment(linker, "/STACK:1024000000,1024000000")#include<iostream>#include<algorithm>#include<ctime>#include<cstdio>#include<cmath>#include<cstring>#include<string>#include<vector>#include<map>#include<set>#include<queue>#include<stack>#include<list>#include<numeric>using namespace std;#define LL long long#define ULL unsigned long long#define INF 0x3f3f3f3f3f3f3f3f#define mm(a,b) memset(a,b,sizeof(a))#define PP puts("*********************");template<class T> T f_abs(T a){ return a > 0 ? a : -a; }template<class T> T gcd(T a, T b){ return b ? gcd(b, a%b) : a; }//template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}// 0x3f3f3f3f3f3f3f3fconst int maxn=550;LL x[maxn],y[maxn];int szx,szy;LL low,high,lcm;LL LCM(LL x,LL y){    LL g=gcd(x,y);    if(x/g>high/y) return high+1;//防止溢出    return x/g*y;}LL Count(LL a,LL n){    return n/a-n/LCM(a,lcm);}void dfs(int i,LL fac,int cnt,LL &sum,LL n){    if(i==szx){        if(cnt==0) return;        if(cnt&1) sum+=Count(fac,n);        else sum-=Count(fac,n);        return;    }    dfs(i+1,fac,cnt,sum,n);    dfs(i+1,LCM(fac,x[i]),cnt+1,sum,n);}int main(){    while(~scanf("%d%d%lld%lld",&szx,&szy,&low,&high)){        if(!szx&&!szy&&!low&&!high)            break;        for(int i=0;i<szx;i++)            scanf("%lld",&x[i]);        lcm=1;        for(int i=0;i<szy;i++){            scanf("%lld",&y[i]);            lcm=LCM(lcm,y[i]);        }        LL sum1=0,sum2=0;        dfs(0,1,0,sum1,high);        dfs(0,1,0,sum2,low-1);        sum1-=sum2;        printf("%lld\n",sum1);    }    return 0;}


原创粉丝点击