Soluție HackerRank pentru Possible Path, subdomeniul Fundamentals, în Python 3. Include cerința formatată, exemple, explicația pașilor și cod sursă.

  • Problemă: Possible Path
  • Domeniu: Fundamentals
  • Limbaj: Python 3

Challenge: Possible Path

Subdomeniu: Fundamentals (fundamentals)

Scor cont: 20.0 / 20

Submission status: Accepted

Submission score: 1.0

Submission ID: 464721596

Limbaj: python3

Link challenge: https://www.hackerrank.com/challenges/possible-path/problem

Cerință

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), text{or} (a, b - a) 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 and y.

Output Format

For each test case, display `YES` or `NO` that indicates if Adam can reach (x,y) or not.

Constraints

- 1 ≤ T ≤ 1000
- 1 ≤ a,b,x,y ≤ 10^18

Cod sursă

#!/bin/python3

import math
import os
import random
import re
import sys

def solve(a, b, x, y):
    if math.gcd(a,b)==math.gcd(x,y):
        return 'YES'
    else:
        return 'NO'
if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    t = int(input().strip())

    for t_itr in range(t):
        first_multiple_input = input().rstrip().split()

        a = int(first_multiple_input[0])

        b = int(first_multiple_input[1])

        x = int(first_multiple_input[2])

        y = int(first_multiple_input[3])

        result = solve(a, b, x, y)

        fptr.write(result + '\n')

    fptr.close()
HackerRank Fundamentals – Possible Path