📌  相关文章
📜  检查是否可以通过分别增加/减少 K1 和 K2 使两个坐标相等

📅  最后修改于: 2022-05-13 01:56:10.150000             🧑  作者: Mango

检查是否可以通过分别增加/减少 K1 和 K2 使两个坐标相等

给定两个整数坐标(X1, Y1)(X2, Y2)以及两个正整数K1K2 ,任务是通过执行以下步骤任意次数来检查两个坐标是否相等:

  • (X1, Y1)的一个或两个坐标中添加或减去K1
  • (X2, Y2)的一个或两个坐标中添加或减去K2

如果可以使(X1, Y1)(X2, Y2)相等,则打印Yes 。否则,打印No

例子:

方法:这个问题可以使用贪心方法来解决,基于观察到(X1, Y1)点可以在 x 方向上采取的移动是n1并且(X2, Y2)点在 x 方向上采取的移动是n2 , 那么表达式可以写成:

类似地,对于 y 方向也可以写成:

现在,可以看出,问题已经被简化为寻找上述方程是否有解。如果两个方程都有非负解,则打印'Yes' 。否则,打印'No'

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to check if both can be merged
int twoPointsReachable(int X1, int Y1, int X2, int Y2,
                       int K1, int K2)
{
 
    // Calculate gcd of K1, K2
    int g = __gcd(K1, K2);
 
    // Solve for the X-axis
    bool reachableOnX = 0;
 
    // Calculate distance between the
    // X-coordinates
    int X_distance = abs(X1 - X2);
 
    // Check the divisibility
    if (X_distance % g == 0) {
        reachableOnX = 1;
    }
 
    // Solve for the Y-axis
    bool reachableOnY = 0;
 
    // Calculate distance on between
    // X coordinates
    int Y_distance = abs(Y1 - Y2);
 
    // Check for the divisibility
    if (Y_distance % g == 0) {
        reachableOnY = 1;
    }
 
    // Check if both solutions exist
    if (reachableOnY && reachableOnX) {
        cout << "Yes"
             << "\n";
    }
    else {
        cout << "No"
             << "\n";
    }
    return 0;
}
 
// Driver Code
int main()
{
    // Given Input
    int X1 = 10, Y1 = 10, X2 = 18;
    int Y2 = 13, K1 = 3, K2 = 4;
 
    // Function Call
    twoPointsReachable(X1, X2, Y1, Y2, K1, K2);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
public class GFG{
     
    static int __gcd(int a, int b)
    {
        return b == 0 ? a : __gcd(b, a % b);
         
    }
 
// Function to check if both can be merged
static int twoPointsReachable(int X1, int Y1, int X2, int Y2,
                    int K1, int K2)
{
 
    // Calculate gcd of K1, K2
    int g = __gcd(K1, K2);
 
    // Solve for the X-axis
    boolean reachableOnX = (g == 0);
 
    // Calculate distance between the
    // X-coordinates
    int X_distance = Math.abs(X1 - X2);
 
    // Check the divisibility
    if (X_distance % g == 0) {
        reachableOnX = (g == 1);
    }
 
    // Solve for the Y-axis
    boolean reachableOnY = (g == 0);
     
    // Calculate distance on between
    // X coordinates
    int Y_distance = Math.abs(Y1 - Y2);
 
    // Check for the divisibility
    if (Y_distance % g == 0) {
        reachableOnY = (g == 1);
    }
 
    // Check if both solutions exist
    if (reachableOnY && reachableOnX) {
        System.out.print("Yes");
    }
    else {
        System.out.print("No");
    }
    return 0;
}
 
// Driver Code
public static void main (String[] args)
{
   
    // Given Input
    int X1 = 10, Y1 = 10, X2 = 18;
    int Y2 = 13, K1 = 3, K2 = 4;
 
    // Function Call
    twoPointsReachable(X1, X2, Y1,
                    Y2, K1, K2);
 
}
}
 
// This code is contributed by shivanisinghss2110


Python3
# python program for the above approach
# Function to check if both can be merged
from math import *
 
 
def twoPointsReachable(X1, Y1, X2, Y2, K1, K2):
 
    # Calculate gcd of K1, K2
    g = gcd(K1, K2)
 
    # Solve for the X-axis
    reachableOnX = 0
 
    # Calculate distance between the
    # X-coordinates
    X_distance = abs(X1 - X2)
 
    # Check the divisibility
    if (X_distance % g == 0):
        reachableOnX = 1
 
    # Solve for the Y-axis
    reachableOnY = 0
 
    # Calculate distance on between
    # X coordinates
    Y_distance = abs(Y1 - Y2)
 
    # Check for the divisibility
    if (Y_distance % g == 0):
        reachableOnY = 1
 
    # Check if both solutions exist
    if (reachableOnY and reachableOnX):
        print("Yes")
    else:
        print("No")
 
    return 0
 
 
# Driver Code
# Given Input
X1 = 10
Y1 = 10
X2 = 18
Y2 = 13
K1 = 3
K2 = 4
 
# Function Call
twoPointsReachable(X1, X2, Y1, Y2, K1, K2)
 
# This code is contributed by anudeep23042002


C#
// C++ program for the above approach
using System;
 
public class GFG {
 
    static int __gcd(int a, int b)
    {
        return b == 0 ? a : __gcd(b, a % b);
    }
 
    // Function to check if both can be merged
    static int twoPointsReachable(int X1, int Y1, int X2,
                                  int Y2, int K1, int K2)
    {
 
        // Calculate gcd of K1, K2
        int g = __gcd(K1, K2);
 
        // Solve for the X-axis
        bool reachableOnX = Convert.ToBoolean(0);
 
        // Calculate distance between the
        // X-coordinates
        int X_distance = Math.Abs(X1 - X2);
 
        // Check the divisibility
        if (X_distance % g == 0) {
            reachableOnX = Convert.ToBoolean(1);
        }
 
        // Solve for the Y-axis
        bool reachableOnY = Convert.ToBoolean(0);
 
        // Calculate distance on between
        // X coordinates
        int Y_distance = Math.Abs(Y1 - Y2);
 
        // Check for the divisibility
        if (Y_distance % g == 0) {
            reachableOnY = Convert.ToBoolean(1);
        }
 
        // Check if both solutions exist
        if (reachableOnY && reachableOnX) {
            Console.Write("Yes");
        }
        else {
            Console.Write("No");
        }
        return 0;
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        // Given Input
        int X1 = 10, Y1 = 10, X2 = 18;
        int Y2 = 13, K1 = 3, K2 = 4;
 
        // Function Call
        twoPointsReachable(X1, X2, Y1, Y2, K1, K2);
    }
}
 
// This code is contributed by shivanisinghss2110


Javascript



输出:
Yes

时间复杂度: O(log(max(K1, K2)))
辅助空间: O(1)