ural 1286. Starship Travel
1286. Starship Travel
Time limit: 1.0 second
Memory limit: 64 MB
Memory limit: 64 MB
It is well known that a starship equipped with class B hyperengine is able to travel from any planet to any other planet. But your starship got severe damage in the last travel and now its movement ability is limited. The starship’s technician determined that with the damaged hyperengine the vehicle can move from a point with coordinates (i,j) only to a point from the following list: (i+q, j+p), (i−q, j+p), (i+q, j−p), (i−q, j−p), (i+p, j+q), (i−p, j+q), (i+p, j−q), (i−p, j−q) (all the coordinates here are integers and are given in the standard intergalaxy system). Help the captain of your ship to find out if the ship is able to reach the destination planet on its own or a repair ship must be called.
Input
The first line contains two integers p and q (the two remaining discrete power rates of the damaged hyperengine) separated with a space. The second line contains the coordinates of the point where the spaceship is now. The third line contains the coordinates of the destination planet. The numbers in the second and third lines are also separated with spaces. All the numbers are integers and do not exceed 2·109 in absolute value.
Output
If the commander can move the damaged starship to the destination planet, write ‘YES’. Write ‘NO’ if a repair ship must be called.
Samples
input | output |
---|---|
4 6 0 0 10 10 |
YES |
4 6 0 0 9 9 |
NO |
Problem Author: Alexander Klepinin
Problem Source: USU Personal Contest 2004
Problem Source: USU Personal Contest 2004
Tags: number theory
Difficulty: 693
题意:看题吧,主要是说给出p,q,以及起始点(sx,sy),结束点(ex, ey),每次可以从(x, y)->(x+-p y+-q)或(x+-q, y +- p),问能不能从起始点到结束点。
分析:首先,因为不管步数,所以x、y的变化量必定都是gcd(p, q)的整数倍
令g = gcd(p, q)
所以如果abs(ex-sx)、abs(ey-sy)不能被g整除,那是不行的。
令x = abs(ex-sx), y = abs(ey - sy)
让x、y、p、q都除以g
就是说现在x、y、p、q都代表最少的次数
那我们假设每次的步伐都变成g,因为这显然是可以的。。。
如果x,y同奇同偶,那么显然可以到达,即使x方向、y方向上的次数不一样,但完全可以+g再-g做两次无用功保持奇偶性不变,并且原地不动
如果p,q不同奇同偶,那么也是可以的,因为p,q一奇一偶,显然可以组成任何想要的数(因为不计次数)
其实网上的博客更好
我的题解有点意识流了。。。
注意处理p == q == 0 的情况
1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cmath> 5 #include <deque> 6 #include <vector> 7 #include <queue> 8 #include <iostream> 9 #include <algorithm> 10 #include <map> 11 #include <set> 12 #include <ctime> 13 using namespace std; 14 typedef long long LL; 15 typedef double DB; 16 #define For(i, s, t) for(int i = (s); i <= (t); i++) 17 #define Ford(i, s, t) for(int i = (s); i >= (t); i--) 18 #define Rep(i, t) for(int i = (0); i < (t); i++) 19 #define Repn(i, t) for(int i = ((t)-1); i >= (0); i--) 20 #define rep(i, x, t) for(int i = (x); i < (t); i++) 21 #define MIT (2147483647) 22 #define INF (1000000001) 23 #define MLL (1000000000000000001LL) 24 #define sz(x) ((int) (x).size()) 25 #define clr(x, y) memset(x, y, sizeof(x)) 26 #define puf push_front 27 #define pub push_back 28 #define pof pop_front 29 #define pob pop_back 30 #define ft first 31 #define sd second 32 #define mk make_pair 33 inline void SetIO(string Name) { 34 string Input = Name+".in", 35 Output = Name+".out"; 36 freopen(Input.c_str(), "r", stdin), 37 freopen(Output.c_str(), "w", stdout); 38 } 39 40 inline int Getint() { 41 int Ret = 0; 42 char Ch = ' '; 43 bool Flag = 0; 44 while(!(Ch >= '0' && Ch <= '9')) { 45 if(Ch == '-') Flag ^= 1; 46 Ch = getchar(); 47 } 48 while(Ch >= '0' && Ch <= '9') { 49 Ret = Ret*10+Ch-'0'; 50 Ch = getchar(); 51 } 52 return Flag ? -Ret : Ret; 53 } 54 55 LL p, q, a, b, c, d; 56 57 inline void Input() { 58 cin>>p>>q>>a>>b>>c>>d; 59 } 60 61 inline int Gcd(int a, int b) { 62 if(b) return Gcd(b, a%b); 63 else return a; 64 } 65 66 inline void Solve() { 67 LL x = abs(a-c), y = abs(b-d), g = Gcd(p, q); 68 if(!g || x%g || y %g) { 69 puts("NO"); 70 return; 71 } 72 x /= g, y /= g, p /= g, q /= g; 73 x &= 1, y &= 1, p &= 1, q &= 1; 74 if(!(x^y) || (p^q)) puts("YES"); 75 else puts("NO"); 76 } 77 78 int main() { 79 #ifndef ONLINE_JUDGE 80 SetIO("D"); 81 #endif 82 Input(); 83 Solve(); 84 return 0; 85 }