ural 1286. Starship Travel
2015-10-26 20:39
225 查看
1286. Starship Travel
Time limit: 1.0 secondMemory 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 Source: USU Personal Contest 2004
Tags: number theory (hide tags for unsolved problems)
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 的情况
#include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <deque> #include <vector> #include <queue> #include <iostream> #include <algorithm> #include <map> #include <set> #include <ctime> using namespace std; typedef long long LL; typedef double DB; #define For(i, s, t) for(int i = (s); i <= (t); i++) #define Ford(i, s, t) for(int i = (s); i >= (t); i--) #define Rep(i, t) for(int i = (0); i < (t); i++) #define Repn(i, t) for(int i = ((t)-1); i >= (0); i--) #define rep(i, x, t) for(int i = (x); i < (t); i++) #define MIT (2147483647) #define INF (1000000001) #define MLL (1000000000000000001LL) #define sz(x) ((int) (x).size()) #define clr(x, y) memset(x, y, sizeof(x)) #define puf push_front #define pub push_back #define pof pop_front #define pob pop_back #define ft first #define sd second #define mk make_pair inline void SetIO(string Name) { string Input = Name+".in", Output = Name+".out"; freopen(Input.c_str(), "r", stdin), freopen(Output.c_str(), "w", stdout); } inline int Getint() { int Ret = 0; char Ch = ' '; bool Flag = 0; while(!(Ch >= '0' && Ch <= '9')) { if(Ch == '-') Flag ^= 1; Ch = getchar(); } while(Ch >= '0' && Ch <= '9') { Ret = Ret*10+Ch-'0'; Ch = getchar(); } return Flag ? -Ret : Ret; } LL p, q, a, b, c, d; inline void Input() { cin>>p>>q>>a>>b>>c>>d; } inline int Gcd(int a, int b) { if(b) return Gcd(b, a%b); else return a; } inline void Solve() { LL x = abs(a-c), y = abs(b-d), g = Gcd(p, q); if(!g || x%g || y %g) { puts("NO"); return; } x /= g, y /= g, p /= g, q /= g; x &= 1, y &= 1, p &= 1, q &= 1; if(!(x^y) || (p^q)) puts("YES"); else puts("NO"); } int main() { #ifndef ONLINE_JUDGE SetIO("D"); #endif Input(); Solve(); return 0; }
View Code
相关文章推荐
- 信息安全系统设计基础第六周学习总结
- android开发之shape详解
- Jquery+div+css实现弹出登录窗口
- android关于popupWindow不显示
- ZOJ 2830
- InnoDB事务的开启
- Canvas画板实现一个简单的球在盒子内随机移动效果
- poj 2155 (二维数组)
- 基于的DCT水印算法实现
- IOS开发笔记10-字符串-C语言笔记
- 实战iOS7之NSURLSession
- Eclipse、IDEA环境下设置jvm默认编码
- LevelDB.NET性能测试
- (原创)c#学习笔记06--函数04--结构函数
- POJ 3415 Common Substrings
- [HDOJ1301]Jungle Roads
- 闭上眼一如既往保持一颗安然又坚定的心!
- openfire源码分析---3
- iOS--sqlite--增删改查
- 《Java 2 实用教程》读书笔记(一)