HDU 4596

Yet another end of the world

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


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
 
題意:有n個蟲洞,問是不是存在一個ID使得這個ID會讓滿足兩個或者兩個以上的蟲洞吸引條件。
思路:判斷一階不定方程有解問題,首先區間有重複肯定是不對的。再就是區間不重複的時候,判斷ID%x1和ID%x2的解是否分別在[y1,z1]和[y2,z2]的情況。
ID = a*x1 + p = b*x2+q;
a*x1-b*x2 = q - p;
也就是說,判斷這個函數的解是否在q - p的區間裏,判斷gcd(x1,x2)是否在[y2 - z1,z2 - y1]中。(先排序,避免複雜判斷
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
struct node
{
    int x,y,z;
    bool operator <(const node&a)const
    {
        if(y!=a.y)return y < a.y;
        return z < a.z;
    }
}num[1024];
int gcd(int a,int b)
{
    return a == 0?b:gcd(b%a,a);
}
bool judge(int i,int j)
{
    int y1 = num[i].y,z1 = num[i].z;
    int y2 = num[j].y,z2 = num[j].z;
    int x1 = num[i].x,x2 = num[j].x;
    if(y1 <= y2&&z1>=y2&&z2>=y1&&z2>=z1)
                    return true;
    if(y1>=y2&&z2>=z1)
                    return true;
    if(y1>=y2&&z2>=y1&&z1>=z2&&z1>=y1)
                    return true;
    if(y2>=y1&&z1>=z2)
                    return true;
    int dif = gcd(x1,x2);
    int l = y2 - z1,r = z2 - y1;
    if((l<=dif&&dif<=r)||(dif<=r - l+1))
        return true;
    return false;
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i = 0;i < n;i++)
        {
            scanf("%d%d%d",&num[i].x,&num[i].y,&num[i].z);
        }
        sort(num,num+n);
        bool ok = 1;
        for(int i = 0;i < n;i++)
        {
            for(int j = i+1;j < n;j++)
            {
                if(judge(i,j))
                    ok = 0;
            }
        }
        if(ok)
            printf("Can Take off\n");
        else
            printf("Cannot Take off\n");
    }
    return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章