ural 1286. Starship Travel

1286. Starship Travel

Time limit: 1.0 second
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+qj+p), (iqj+p), (i+qjp), (iqjp), (i+pj+q), (ipj+q), (i+pjq), (ipjq) (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

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 的情况

 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 }

时间: 2024-08-25 20:36:11

ural 1286. Starship Travel的相关文章

URAL 1900. Brainwashing Device(dp+输出路径)

1900. Brainwashing Device Time limit: 1.0 second Memory limit: 64 MB While some people travel in space from planet to planet and discover new worlds, the others who live on Earth still have to get up in the morning, go to work, return back home and t

Ural 1004 Sightseeing Trip

Sightseeing Trip Time Limit: 2000ms Memory Limit: 16384KB This problem will be judged on Ural. Original ID: 100464-bit integer IO format: %lld      Java class name: (Any) There is a travel agency in Adelton town on Zanzibar island. It has decided to

HDU 1011 Starship Troopers(树形dp+背包)

Starship Troopers Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13109    Accepted Submission(s): 3562 Problem Description You, the leader of Starship Troopers, are sent to destroy a base of

Ural 1081 Binary Lexicographic Sequence(DP)

题目地址:Ural 1081 先用dp求出每个长度下的合法序列(开头为1)的个数.然后求前缀和.会发现正好是一个斐波那契数列.然后每次判断是否大于此时长度下的最少个数,若大于,说明这一位肯定是1,若小于,则肯定是0.就这样不断输出出来即可. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #in

UVA 1048 - Low Cost Air Travel(最短路)

UVA 1048 - Low Cost Air Travel 题目链接 题意:给定一些联票,在给定一些行程,要求这些行程的最小代价 思路:最短路,一张联票对应几个城市就拆成多少条边,结点表示的是当前完成形成i,在城市j的状态,这样去进行最短路,注意这题有坑点,就是城市编号可能很大,所以进行各种hash 代码: #include <cstdio> #include <cstring> #include <vector> #include <queue> #in

URAL 1684. Jack&#39;s Last Word KMP

题目来源:URAL 1684. Jack's Last Word 题意:输入a b 把b分成若干段 每一段都是a的前缀 思路:b为主串 然后用a匹配b 记录到b的i位置最大匹配的长度 然后分割 分割的时候要从后往前 如果a = abac b = abab 那么如果从前往后 首先覆盖了aba 然后b就不能覆盖了 从后往前就可以了 首先覆盖ab 下一次还是ab 因为已经记录了到i位置的最大匹配长度 根据长度从末尾倒退 每次倒退的时候只要是最大的匹配的长度 因为如果在某一次的递推 记录的最大匹配的前缀

Travel(最短路)

Travel The country frog lives in has nn towns which are conveniently numbered by 1,2,…,n1,2,…,n. Among n(n−1)2n(n−1)2 pairs of towns, mm of them are connected by bidirectional highway, which needs aa minutes to travel. The other pairs are connected b

URAL 1205 By the Underground or by Foot?(SPFA)

By the Underground or by Foot? Time limit: 1.0 secondMemory limit: 64 MB Imagine yourself in a big city. You want to get from point A to point B. To do that you may move by foot or use the underground. Moving by the underground is faster but you may

Travel(HDU 5441 2015长春区域赛 带权并查集)

Travel Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2404    Accepted Submission(s): 842 Problem Description Jack likes to travel around the world, but he doesn’t like to wait. Now, he is tr