hdu Yet another end of the world(擴充套件歐幾里得定理推論)
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.
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).
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
相關文章
- POJ1061擴充套件歐幾里得定理套件
- 擴充套件歐幾里得的幾個定理以及證明套件
- 擴充套件歐幾里得套件
- 擴充套件歐幾里得演算法公式快速推導套件演算法公式
- 湘潭大學四月月賽C題A+B(擴充套件歐幾里得定理)套件
- 歐幾里得演算法與擴充套件歐幾里得演算法演算法套件
- 擴充套件中國剩餘定理套件
- 數論學習筆記 (4):擴充套件歐幾里得演算法筆記套件演算法
- 淺談擴充套件歐幾里得演算法套件演算法
- Google釋出Chrome加密擴充套件End-To-EndGoChrome加密套件
- 擴充套件中國剩餘定理詳解套件
- Yet Another Problem
- 尤拉函式、整除分塊和擴充套件歐幾里得函式套件
- 51nod 1352 集合計數(擴充套件歐幾里得)套件
- 數論入門基礎(同餘定理/費馬小定理/擴充套件歐幾里德演算法/中國剩餘定理)套件演算法
- manacher || 擴充套件kmp -- Best Reward HDU - 3613套件KMP
- PHP擴充套件開發教程2 – 編寫第一個擴充套件 hello worldPHP套件
- 數論分塊擴充套件套件
- Yet Another Intro for SymbolSymbol
- Yet Another Permutation ConstructiveStruct
- 擴充套件中國剩餘定理(EXCRT)學習筆記套件筆記
- 菜鳥學php擴充套件 之 hello world(一)PHP套件
- HDU 3234 Exclusive-OR 擴充套件並查集套件並查集
- [擴充套件推薦]Aliyun-oss-laravel —— Laravel最好的OSS Storage擴充套件套件Laravel
- A - Yet Another Two Integers Problem ACMACM
- Yet Another Intro to Event LoopOOP
- Yet another intro for localStorage and sessionStorageSession
- kotlin 擴充套件(擴充套件函式和擴充套件屬性)Kotlin套件函式
- 討論:何謂擴充套件用例?套件
- D. Yet Another Monster Fight
- 擴充盧卡斯定理 / exlucas
- [譯] 論資料流的擴充套件性套件
- WCF擴充套件:行為擴充套件Behavior Extension套件
- 推薦 | 超讚的Chome擴充套件收集套件
- Laravel 推薦好用擴充套件包整合版Laravel套件
- [擴充套件推薦]簡體轉繁體/繁體轉簡體 OpenCC-PHP 擴充套件套件PHP
- 【Kotlin】擴充套件屬性、擴充套件函式Kotlin套件函式
- 第六章 數學問題 -------- 6.5 歐幾里得演算法及其擴充套件演算法套件