hdu Yet another end of the world(扩张欧几里得定理推论)

hdu Yet another end of the world(扩展欧几里得定理推论)

Yet another end of the world

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 266    Accepted Submission(s): 149


Problem Description
In the year 3013, it has been 1000 years since the previous predicted rapture. However, the Maya will not play a joke any more and the Rapture finally comes in. Fortunately people have already found out habitable planets, and made enough airships to convey all the human beings in the world. A large amount of airships are flying away the earth. People all bear to watch as this planet on which they have lived for millions of years. Nonetheless, scientists are worrying about anther problem…
As we know that long distance space travels are realized through the wormholes, which are given birth by the distortion of the energy fields in space. Airships will be driven into the wormholes to reach the other side of the universe by the suction devices placed in advance. Each wormhole has its configured attract parameters, X, Y or Z. When the value of ID%X is in [Y,Z], this spaceship will be sucked into the wormhole by the huge attraction. However, the spaceship would be tear into piece if its ID meets the attract parameters of two wormholes or more at the same time. 
All the parameters are carefully adjusted initially, but some conservative, who treat the Rapture as a grain of truth and who are reluctant to abandon the treasure, combine with some evil scientists and disrupt the parameters. As a consequence, before the spaceships fly into gravity range, we should know whether the great tragedy would happen or not. Now the mission is on you.
 

Input
Multiple test cases, ends with EOF.
In each case, the first line contains an integer N(N<=1000), which means the number of the wormholes. 
Then comes N lines, each line contains three integers X,Y,Z(0<=Y<=Z<X<2*109).
 

Output
If there exists danger, output “Cannot Take off”, else output “Can Take off”.
 

Sample Input
2 7 2 3 7 5 6 2 7 2 2 9 2 2
 

Sample Output
Can Take off Cannot Take off
 

Source
2013 ACM-ICPC南京赛区全国邀请赛——题目重现
 



题目大意:给定1000个xi,yi,zi,问是否存在一组i,j使得t%xi=[yi,zi],t%xj=[yj,zj].比赛的时候应该自己多在纸上画一下的,其实这类题目自己开始就做过两个,而且比这个复杂一些,那个需要求解,但是这个无需求解。

  解题思路:比赛一直在想着怎么过掉C题。。。t=a1*x1+b1,t=a2*x2+b2,可以转换成a1*x1+a2*x2=b2-b1,这个同余方程有解的前提是(b2-b1)%gcd(x1,x2)==0,当时还看了这方面的相关证明以及如何用扩展欧几里得求解。根据这个结论就很容易求解了,看yi,zi,yj,zj中间是否有解使得%gcd(xi,xj)==0。

  题目地址:Yet another end of the world

AC代码:
#include<iostream>
#include<cstdio>
using namespace std;
int p[1002][3];  //分别代表x,y,z

int gcd(int m,int n)
{
    int tmp;
    while(n)
    {
        tmp=m%n;
        m=n;
        n=tmp;
    }
    return m;
}

int check(int a,int b)
{
    int d=gcd(p[a][0],p[b][0]);
    if(p[a][1]>p[b][1]) swap(a,b);
    int y1,z1,y2,z2;
    y1=p[a][1],z1=p[a][2],y2=p[b][1],z2=p[b][2];

    if(y2<=z1) return 1;  //说明两个区间有交叉,可以为0
    int s1,s2; //s1-s2即为模可以取值的空间
    s1=y2-z1,s2=z2-y1;
    if(s1%d==0) return 1;

    int tmp=s1/d;
    tmp=d*(tmp+1);
    if(tmp<=s2) return 1;
    return 0;
}

int main()
{
    int n,i,j;
    while(cin>>n)
    {
        for(i=0;i<n;i++)
            for(j=0;j<3;j++)
                scanf("%d",&p[i][j]);

        int flag=0;
        for(i=0;i<n;i++)
        {
            for(j=i+1;j<n;j++)
                if(check(i,j))
                {
                    flag=1;
                    break;
                }
            if(flag) break;
        }
        if(flag) puts("Cannot Take off");
        else puts("Can Take off");
    }
    return 0;
}

/*
2
7 2 3
7 5 6
*/
//781MS