Yet another end of the world
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 696 Accepted Submission(s): 325
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*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
2013 ACM-ICPC南京赛区全国邀请赛——题目重现
题意:一开始是真的没看懂题目。。。。
求是否存在一个ID整数,使得ID % X[i] 属于(y[i],z[i]) , ID % X [j] 属于(y[j],z[j]);且i不等于j。若存在,则“cannot”,不存在则“can”。
令a = ID % X[i],b = ID % X[j],设未知数m1,m2
即题意就是判断下列方程组是否有解:
a = ID - X[i] * m1; /1
b = ID - X[j] * m2; /2
Y[i] <= a <= Z[i]; /3
Y[j] <= b < =Z[j]; /4
1式减去2式,得
a - b = X[i] * m1 - X [j] * m2;
这就是一个二元一次不定方程,若该方程有解,则满足(a - b ) % (gcd(X[i],X[j])) == 0; /5
所以这个题就是求出a - b范围后,枚举a - b 的值,看是否存在一个值满足上述方程5式,满足则“cannot”
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int N,i,j,a,b;
int X[1005],Y[1005],Z[1005];
bool flag;
int gcd (int n,int m)
{
return (m ? gcd(m,n % m) : n );
}
bool judge(int i,int j)
{
int nmin,nmax,k,m;
nmin = Y[i] - Z[j];
nmax = Z[i] - Y[j];
k = gcd(X[i],X[j]);
/*for(m = nmin;m <= nmax;m++)
{
if(m % (gcd(X[i],X[j])) == 0) return true;
}*/
//判断是否存在a-b满足方程5式,下列if语句算法比注释内的for语句算法大概能节约一半的时间
if(nmin % k == 0 || nmax % k == 0)
return true;
else
return nmin/k != nmax/k;
}
int main()
{
while(cin >> N )
{
flag = true;
for(i = 0; i < N; i++)
{
cin >> X[i] >> Y[i] >> Z[i];
}
for(i = 0; i < N; i++)
for(j = i + 1; j < N; j++)
{
if(judge(i,j))
flag = false;
goto fend;
//可不加goto语句,对时间影响不大
}
fend : puts(flag ?"Can Take off":"Cannot Take off");
}
return 0 ;
}