HDU 4596Yet another end of the world(GCD)

来源:互联网 发布:阿里云香港翻墙 编辑:程序博客网 时间:2024/05/16 17:39

题意:给出N行  x,l,r。如果有数ID%x1属于[l1,r1]而且ID%x2属于[l2,r2]那么Cannot Take off,否则Can Take off。

分析:假设有这么一个数ID使得“cannot.....”,那么:

ID=x1*a+u ①

且ID=x2*b+v ②

②-①:x2*b-x1*a=v-u ③

此时 l2-r1<=x2*b-x1*a<=r2-l1

如果③有解则gcd(x1,x2)的n倍在区间[l2-r1,r2-l1]。

有解则说明假设成立,否则假设不成立ID则使得“can....”。

#include<iostream>#include<string>#include<stdio.h>#include<string.h>#include<vector>#include<math.h>#include<queue>#include<map>#include<set>#include<algorithm>using namespace std;#define MAXN 1005#define LL long long#define INF 0x7f7f7f7fconst double eps = 1e-10;LL n;struct node{    LL x,l,r;}a[MAXN],tx,ty;LL fun(LL l1,LL r1,LL l2,LL r2){    if(l2>=l1&&l2<=r1||r2>=l1&&r2<=r1)        return 1;    if(l1>=l2&&l1<=r2||r1>=l2&&r1<=r2)        return 1;    return 0;}LL gcd(LL a,LL b){    return b==0?a:gcd(b,a%b);}LL ok(LL i,LL j){    LL ll,rr;    if(a[i].x>a[j].x)    {        tx=a[i];        ty=a[j];    }    else tx=a[j],ty=a[i];    if(fun(tx.l,tx.r,ty.l,ty.r))        return 0;    LL d=gcd(tx.x,ty.x);    if(ty.r<tx.l)    {        ll=tx.l-ty.r;        rr=tx.r-ty.l;    }    else    {        ll=ty.l-tx.r;        rr=ty.r-tx.l;    }    if(rr-ll+1>=d)        return 0;    else    {        LL modl=ll%d;        LL modr=rr%d;        if(modl==0)            return 0;        else        if(modl<modr)        {           return 1;        }        else if(modr<modl)        {            return 0;        }        else if(modl==modr)        {            if(modl==0)                return 0;        }    }}int main(){    LL j,i;    while(scanf("%d",&n)!=EOF)    {        for(i=1;i<=n;i++)            scanf("%lld%lld%lld",&a[i].x,&a[i].l,&a[i].r);        LL ans=0;        for(i=1;i<=n;i++)        {            for(j=i+1;j<=n;j++)            {                if(!ok(i,j))                {                    ans=1;                    break;                }            }            if(ans==1)break;        }        if(ans==1)            puts("Cannot Take off");        else puts("Can Take off");    }    return 0;}





0 0