📌  相关文章
📜  通过将它们除以它们的因子来检查 X 和 Y 是否可以在 N 步中相等

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

通过将它们除以它们的因子来检查 X 和 Y 是否可以在 N 步中相等

给定正整数XYN ,任务是检查是否可以在恰好N 次操作中使X等于Y ,其中每个操作:

  • X可以除以除 1 之外的任何因子。
  • Y可以除以除 1 以外的任何因子。

例子:

方法:解决问题的想法基于以下观察:

按照下面提到的步骤来实现上述观察:

  • 如果N等于 1:
    • 如果XY中的一个是另一个的因子,那么它们可以相等。
    • 否则,它们不能相等。
  • 否则,如果 N 小于或等于 X 和 Y 的最大因子数,则可以使它们相等。
  • 否则,没有解决方案是可能的。

以下是以优化的方式实现上述方法:

C++
// C++ code to implement above approach
 
#include 
using namespace std;
 
// Function to find total
// number of times we can
// divide a number before making it 1.
int count(int val)
{
    int ans = 0;
 
    // Checking how many times
    // the number can be
    // divided by 2.
    while (val % 2 == 0) {
        ans++;
        val = val / 2;
    }
 
    // Iterating through each number from
    // 3 to sqrt(val) and finding how many
    // times the number can be divide
    for (int i = 3; i <= sqrt(val); i = i + 2) {
        while (val % i == 0) {
            ans++;
            val = val / i;
        }
    }
    if (val > 2)
        ans++;
 
    return ans;
}
 
// Function to return if
// we can make x aand y equal or not
int solve(int x, int y, int n)
{
    if (n == 0) {
        if (x == y) {
            cout << "Yes";
        }
        else {
            cout << "No";
        }
    }
 
    else if (n == 1) {
        if ((x % y == 0 || y % x == 0)
            && x != y) {
            cout << "Yes";
        }
        else {
            cout << "No";
        }
    }
 
    else {
        if (count(x) + count(y) >= n) {
            cout << "Yes";
        }
        else {
            cout << "No";
        }
    }
}
 
// Driver code
int main()
{
    int X = 4, Y = 21, N = 3;
 
    // Function call
    solve(X, Y, N);
    return 0;
}


Java
// Java code to implement above approach
import java.io.*;
 
class GFG
{
 
  // Function to find total
  // number of times we can
  // divide a number before making it 1.
  static int count(int val)
  {
    int ans = 0;
 
    // Checking how many times
    // the number can be
    // divided by 2.
    while (val % 2 == 0) {
      ans++;
      val = val / 2;
    }
 
    // Iterating through each number from
    // 3 to sqrt(val) and finding how many
    // times the number can be divide
    for (int i = 3; i <= Math.sqrt(val); i = i + 2) {
      while (val % i == 0) {
        ans++;
        val = val / i;
      }
    }
    if (val > 2)
      ans++;
 
    return ans;
  }
 
  // Function to return if
  // we can make x aand y equal or not
  static void solve(int x, int y, int n)
  {
    if (n == 0) {
      if (x == y) {
        System.out.print("Yes");
      }
      else {
        System.out.print("No");
      }
    }
 
    else if (n == 1) {
      if ((x % y == 0 || y % x == 0)
          && x != y) {
        System.out.print("Yes");
      }
      else {
        System.out.print("No");
      }
    }
 
    else {
      if (count(x) + count(y) >= n) {
        System.out.print("Yes");
      }
      else {
        System.out.print("No");
      }
    }
  }
 
  // Driver code
  public static void main (String[] args) {
    int X = 4, Y = 21, N = 3;
 
    // Function call
    solve(X, Y, N);
  }
}
 
// This code is contributed by hrithikgarg03188.


Python3
# Python3 code to implement above approach
 
# Function to find total
# number of times we can
# divide a number before making it 1.
def count(val):
 
    ans = 0
 
    # Checking how many times
    # the number can be
    # divided by 2.
    while (val % 2 == 0):
        ans += 1
        val = val // 2
 
    # Iterating through each number from
    # 3 to sqrt(val) and finding how many
    # times the number can be divide
    for i in range(3, int(val**0.5)+1, 2):
        while (val % i == 0):
            ans += 1
            val = val // i
 
    if (val > 2):
        ans += 1
 
    return ans
 
 
# Function to return if
# we can make x aand y equal or not
def solve(x,  y,  n):
 
    if (n == 0):
        if (x == y):
            print("Yes")
 
        else:
            print("No")
 
    elif (n == 1):
        if ((x % y == 0 or y % x == 0) and x != y):
            print("Yes")
        else:
            print("No")
    else:
        if (count(x) + count(y) >= n):
            print("Yes")
        else:
            print("No")
 
# Driver code
if __name__ == "__main__":
    x = 4
    y = 21
    n = 3
    solve(x, y, n)
 
    # This code is contributed by amnindersingh1414.


C#
// C# code to implement above approach
using System;
public class GFG {
 
  // Function to find total
  // number of times we can
  // divide a number before making it 1.
  static int count(int val)
  {
    int ans = 0;
 
    // Checking how many times
    // the number can be
    // divided by 2.
    while (val % 2 == 0) {
      ans++;
      val = val / 2;
    }
 
    // Iterating through each number from
    // 3 to sqrt(val) and finding how many
    // times the number can be divide
    for (int i = 3; i <= Math.Sqrt(val); i = i + 2) {
      while (val % i == 0) {
        ans++;
        val = val / i;
      }
    }
    if (val > 2)
      ans++;
 
    return ans;
  }
 
  // Function to return if
  // we can make x aand y equal or not
  static void solve(int x, int y, int n)
  {
    if (n == 0) {
      if (x == y) {
        Console.Write("Yes");
      }
      else {
        Console.Write("No");
      }
    }
 
    else if (n == 1) {
      if ((x % y == 0 || y % x == 0) && x != y) {
        Console.Write("Yes");
      }
      else {
        Console.Write("No");
      }
    }
 
    else {
      if (count(x) + count(y) >= n) {
        Console.Write("Yes");
      }
      else {
        Console.Write("No");
      }
    }
  }
 
  // Driver code
  public static int Main()
  {
    int X = 4, Y = 21, N = 3;
 
    // Function call
    solve(X, Y, N);
    return 0;
  }
}
 
// This code is contributed by Taranpreet


Javascript


输出
Yes

时间复杂度: O(√X)+ √Y)
辅助空间: O(1)