hdu Yet another end of the world(擴充套件歐幾里得定理推論)

果7發表於2013-10-28

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
 



題目大意:給定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。


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



相關文章