Yet another end of the world
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 357 Accepted Submission(s): 182
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.
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*10 9).
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*10 9).
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
分析:1. 假设只有两组分别为x1,y1,z1和x2,y2,z2,对于给出的条件,应该有
a*x1+a'+y1 <= ID <=a*x1+a'+y2,b*x2+b'+y2 <= ID <= b*x2+b'+y2;
(解释一下,由于题意是问ID%x1∈[ y[1],z[1] ],不妨假设存在一个数a=ID / x1,a'=ID % x1 ,于是可以得到如上表达式)
2. 化简得:y2-z1 <= a*x1-b*x2 <= z2-y1。
3. 若a,b有整数解,根据扩展欧几里得定理:ax1+bx2 = u 有整数解的情况为u / gcd(x1,x2) == 0;
所以若存在y2-z1≤u≤z2-y1,使得u / gcd(x1,x2) == 0即可。
具体做法:(有一些注意点见题中代码)
首先看到N的范围是1000,也就是可以暴力枚举一下给出的每个区间之间的关系
代码如下:
#include<iostream>
#include<stdio.h>
using namespace std;
int n,x[1010],y[1010],z[1010];
int gcd(int a,int b)
{
return b==0?a:gcd(b,a%b);
}
int judge(int g,int a,int b)
{
if(a%g == 0||b%g == 0) return 1;//两个交集点正好能整除g
//if(a < 0&&b > 0) return 1;//交集存在:这条可有可无
if(b/g - a/g >= 1) return 1;
//等式变为a/g <= u/g <= b/g,此时有解的情况就是上面等式
return 0;
}
int solve()
{
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
int g = gcd(x[i],x[j]);
if(judge(g,y[j]-z[i],z[j]-y[i])) return 0;
//if exists ID then the worm is not safe
}
}
return 1;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
for(int i=0;i<n;i++)
scanf("%d%d%d",&x[i],&y[i],&z[i]);
int ans=solve();
if(ans==0)
printf("Cannot Take off\n");
if(ans==1)
printf("Can Take off\n");
}
return 0;
}