-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathPossible Path.py
More file actions
51 lines (39 loc) · 1.44 KB
/
Possible Path.py
File metadata and controls
51 lines (39 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# -*- coding: utf-8 -*-
"""
Problem Statement
Adam is standing at point (a,b) in an infinite 2D grid. He wants to know if he can reach point (x,y) or not. The only
operation he can do is to move to point (a+b,b), (a,a+b), (a-b,b), or (a,a-b) from some point (a,b). It is given that he
can move to any point on this 2D grid,i.e., the points having positive or negative X(or Y) co-ordinates.
Tell Adam whether he can reach (x,y) or not.
Input Format
The first line contains an integer, T, followed by T lines, each containing 4 space separated integers i.e. a b x y
Output Format
For each test case, display YES or NO that indicates if Adam can reach (x,y) or not.
"""
__author__ = 'Danyang'
class Solution(object):
def solve(self, cipher):
"""
Algorithm: math: https://hr-filepicker.s3.amazonaws.com/infinitum-jun14/editorials/2372-possible-path.pdf
:param cipher: the cipher
"""
a, b, x, y = cipher
if self.gcd(a, b) == self.gcd(x, y):
return "YES"
else:
return "NO"
def gcd(self, a, b):
while b:
a, b = b, a % b
return a
if __name__ == "__main__":
import sys
f = open("1.in", "r")
# f = sys.stdin
testcases = int(f.readline().strip())
for t in xrange(testcases):
# construct cipher
cipher = map(int, f.readline().strip().split(' '))
# solve
s = "%s\n" % (Solution().solve(cipher))
print s,