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

📅  最后修改于: 2021-04-21 23:08:02             🧑  作者: Mango

给定两个整数XY ,任务是检查是否可以通过以下可能的移动从(1,1 )到达(X,Y)

  • 从点(a,b)使得b> a ,移至点(a,b – a)
  • 从a > b的点(a,b)移至(a – b,b)的点
  • 从任意点(a,b)移至点(2 * a,b)(a,2 * b)

如果可以到达(X,Y),则打印“”。否则,打印“否”

例子:

天真的方法:解决问题的最简单方法是通过从某个点进行所有可能的移动来递归检查是否可以从(X,Y)到达( 1,1) 。如果在任何一点都到达了点(1,1) ,则打印“是” 。否则,打印“否”

时间复杂度: O(N 4 ),其中N是到达点(1,1)的步数。
辅助空间: O(1)

高效的方法:这个想法是找到最大的公约数并观察以下事实:

  • 通过将点从(a,b)移至点(a,b – a)(a – b,b) ,该对的GCD不变。
  • 通过将点从(a,b)移至点(2 * a,b)(a,2 * b) ,GCD可能会加倍或保持不变。
  • 因此,根据上述观察,当且仅当(X,Y)的gcd为2的幂时,点(1,1)才能移动到点(X,Y)

请按照以下步骤解决上述方法:

  1. 找到(X,Y)的gcd并将其存储在变量中,例如val
  2. 通过检查(val&(val-1))是否等于0(其中&是按位与) ,检查val是否为2的幂。
  3. 如果是两个的幂,则打印“是” 。否则,打印“否”

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the gcd of
// two numbers
int gcd(int a, int b)
{
     
    // Base case
    if (a < b)
    {
        int t = a;
        a = b;
        b = t;
    }
    if (a % b == 0)
        return b;
         
    // Recurse
    return gcd(b, a % b);
}
 
// Funtion to print the answer
void printAnswer(int x, int y)
{
     
    // GCD of X and Y
    int val = gcd(x, y);
 
    // If GCD is power of 2
    if ((val & (val - 1)) == 0)
        cout << "Yes";
    else
        cout << "No";
}
 
// Driver code
int main()
{
     
    // Given X and Y
    int x = 4;
    int y = 7;
 
    // Function call
    printAnswer(x, y);
 
    return 0;
}
 
// This code is contributed by RohitOberoi


Java
// Java program for the above approach
 
import java.io.*;
 
class GFG {
 
    // Function to find the gcd of two numbers
    public static int gcd(int a, int b)
    {
 
        // Base case
        if (a < b) {
            int t = a;
            a = b;
            b = t;
        }
        if (a % b == 0)
            return b;
 
        // Recurse
        return gcd(b, a % b);
    }
 
    // Funtion to print the answer
    static void printAnswer(int x, int y)
    {
 
        // GCD of X and Y
        int val = gcd(x, y);
 
        // If GCD is power of 2
        if ((val & (val - 1)) == 0)
            System.out.println("Yes");
        else
            System.out.println("No");
    }
 
    // Driver code
    public static void main(String[] args)
    {
 
        // Given X and Y
        int x = 4;
        int y = 7;
 
        // Function call
        printAnswer(x, y);
    }
}


Python3
# Python3 program for the
# above approach
 
# Function to find the gcd
# of two numbers
def gcd(a, b):
 
    # Base case
    if (a < b):
 
        t = a
        a = b
        b = t
 
    if (a % b == 0):
        return b
 
    # Recurse
    return gcd(b, a % b)
 
# Function to print the
# answer
def printAnswer(x, y):
 
    # GCD of X and Y
    val = gcd(x, y)
 
    # If GCD is power of 2
    if ((val &
        (val - 1)) == 0):
        print("Yes")
    else:
        print("No")
 
# Driver code
if __name__ == "__main__":
 
    # Given X and Y
    x = 4
    y = 7
 
    # Function call
    printAnswer(x, y)
 
# This code is contributed by Chitranayal


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the gcd of two numbers
public static int gcd(int a, int b)
{
     
    // Base case
    if (a < b)
    {
        int t = a;
        a = b;
        b = t;
    }
    if (a % b == 0)
        return b;
         
    // Recurse
    return gcd(b, a % b);
}
 
// Funtion to print the answer
static void printAnswer(int x, int y)
{
     
    // GCD of X and Y
    int val = gcd(x, y);
 
    // If GCD is power of 2
    if ((val & (val - 1)) == 0)
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
}
 
// Driver code
public static void Main()
{
     
    // Given X and Y
    int x = 4;
    int y = 7;
 
    // Function call
    printAnswer(x, y);
}
}
 
// This code is contributed by bgangwar59


Javascript


输出:
Yes

时间复杂度: O(Log(min(X,Y)))其中(X,Y)是给定点。
辅助空间: O(1)