Yet another end of the world
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
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
题目大意:给定n组x,y,z,判断是否存在一个id使得:id%x∈[y,z] 同时对2组及以上的x,y,z成立?
完全不知道怎么做,队友看出来用扩展欧几里德,给出如何判断,才写过。。。
由于只要至少有两组x,y,z使得id%x∈[x,y]就危险,所以只判断任意两组x,y,z是否使得其危险即可
假设两组x,y,z分别为:x1,y1,z1和x2,y2,z2
设存在整数a,b,c,d使得:
a*x1+c=id ① (c∈[y1,z1] )
b*x2+d=id ② (d∈[y2,z2] )
由①②得:x1*a-x2*b=b-c
又:方程a*x+b*y=c在整数范围内有解的充要条件是(a,b)整除c
可得:若(b-c)%gcd(x1,x2)==0,则存在整数a,b,c,d使得①②同时成立,即危险;否则,不存在整数a,b,c,d使得①②同时成立,即不危险
#include <cstdio>
using namespace std;
const int MAXN=1005;
int n;
long long x[MAXN],y[MAXN],z[MAXN],l,r,gd;
long long gcd(long long a,long long b){
return b>0?gcd(b,a%b):a;
}
bool judge(int i,int j) {//判断第i,j
gd=gcd(x[i],x[j]);
r=z[j]-y[i];//d-c的上限
l=y[i]-z[j];//d-c的下限
if((l==0&&r==0)||r-l>=gd)//若d-c只能取0,则0%gd==0成立;或者d-c的取值范围 >= gd,则存在d-c使得gd乘除d-c;刚开始写成l==r也AC了(似乎l==r时,只能等于0)
return true;
return l/gd*gd==l || l/gd!=r/gd;//判断d-c的某一倍数是否在区间[l,r];最开始没有判断左端点是否能整除gd,但是依旧AC了
}
inline bool solve() {
for(int i=1;i<n;++i)
for(int j=0;j<i;++j)
if(judge(i,j))
return false;
return true;
}
int main() {
while(scanf("%d",&n)!=EOF){
for(int i=0;i<n;i++)
scanf("%I64d%I64d%I64d",x+i,y+i,z+i);
printf("%s\n",solve()?"Can Take off":"Cannot Take off");
}
return 0;
}