📌  相关文章
📜  通过给定步骤检查是否可以从(1,0)到达(X,Y)

📅  最后修改于: 2021-04-23 06:17:32             🧑  作者: Mango

给定两个正整数XY ,任务是检查是否可以通过给定的步骤从(1,0)达到(X,Y) 。在每个步骤中,来自任何单元格(a,b)的可能移动是(a,b + a)(a + b,b) 。如果可能,打印“是” 。否则,打印“否”

例子:

天真的方法:最简单的方法是尝试使用操作(X – Y,Y)(X,Y – X)递归地从点(X,Y)移至(1,0) 直到等于( 1,0) 。如果X坐标小于1Y坐标小于0 ,则不可能达到(1,0) 。因此,打印“否” 。否则,如果达到(1,0) ,则打印“是”

时间复杂度: O(2 log(min(X,Y)) )
辅助空间: O(1)

高效方法:这个想法是观察以下属性:

  • 尝试以相反的顺序解决问题,即可以通过隐性地移动到点(X-Y,Y)(X,Y-X)(X,Y)移至(1,0 )
  • 上面的属性可以表示为:

因此,根据以上观察,如果GCD(X,Y)= 1,则始终存在从(1,0)(X,Y)的路径。如果给定的两个数字的GCD为1,则打印“是”。否则,打印“否”

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the GCD of two
// numbers a and b
int GCD(int a, int b)
{
    // Base Case
    if (b == 0)
        return a;
 
    // Recursively find the GCD
    else
        return GCD(b, a % b);
}
 
// Function to check if (x, y) can be
// reached from (1, 0) from given moves
void check(int x, int y)
{
    // If GCD is 1, then print "Yes"
    if (GCD(x, y) == 1) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
}
 
// Driver Code
int main()
{
    // Given X and Y
    int X = 2, Y = 7;
 
    // Function call
    check(X, Y);
 
    return 0;
}


Java
// Java program for the
// above approach
import java.util.*;
class GFG{
 
// Function to find the
// GCD of two numbers a
// and b
static int GCD(int a,
               int b)
{
  // Base Case
  if (b == 0)
    return a;
 
  // Recursively find
  // the GCD
  else
    return GCD(b, a % b);
}
 
// Function to check if
// (x, y) can be reached
// from (1, 0) from given
// moves
static void check(int x,
                  int y)
{
  // If GCD is 1, then
  // print "Yes"
  if (GCD(x, y) == 1)
  {
    System.out.print("Yes");
  }
  else
  {
    System.out.print("No");
  }
}
 
// Driver Code
public static void main(String[] args)
{
  // Given X and Y
  int X = 2, Y = 7;
 
  // Function call
  check(X, Y);
}
}
 
// This code is contributed by shikhasingrajput


Python3
# Python3 program for the above approach
 
# Function to find the GCD of two
# numbers a and b
def GCD(a, b):
     
    # Base Case
    if (b == 0):
        return a
         
    # Recursively find the GCD
    else:
        return GCD(b, a % b)
 
# Function to check if (x, y) can be
# reached from (1, 0) from given moves
def check(x, y):
     
    # If GCD is 1, then pr"Yes"
    if (GCD(x, y) == 1):
        print("Yes")
    else:
        print("No")
 
# Driver Code
if __name__ == '__main__':
     
    # Given X and Y
    X = 2
    Y = 7
 
    # Function call
    check(X, Y)
 
# This code is contributed by mohit kumar 29


C#
// C# program for the
// above approach
using System;
 
class GFG{
   
// Function to find the
// GCD of two numbers a
// and b
static int GCD(int a, int b)
{
   
  // Base Case
  if (b == 0)
    return a;
 
  // Recursively find
  // the GCD
  else
    return GCD(b, a % b);
}
 
// Function to check if
// (x, y) can be reached
// from (1, 0) from given
// moves
static void check(int x, int y)
{
   
  // If GCD is 1, then
  // print "Yes"
  if (GCD(x, y) == 1)
  {
    Console.WriteLine("Yes");
  }
  else
  {
    Console.WriteLine("No");
  }
}
 
// Driver Code
public static void Main()
{
   
  // Given X and Y
  int X = 2, Y = 7;
 
  // Function call
  check(X, Y);
}
}
 
// This code is contributed by SURENDRA_GANGWAR


Javascript


输出:
Yes

时间复杂度: O(log(min(X,Y))
辅助空间: O(1)