HDU 4596 Yet another end of the world(GCD)

题意:给出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 0x7f7f7f7f
const 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值