📌  相关文章
📜  通过乘以它们的素因数最小次数使两个数字相等

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

通过乘以它们的素因数最小次数使两个数字相等

给定两个数字XY ,任务是通过重复乘以它们的素因数最小次数使它们相等。

例子:

做法:这个想法是,XY可以是相等仅当X的每一个主要因素是存在于YY的每一个主要因素是存在于X。要计算使它们相等所需的最少操作,请执行以下步骤:



  • 计算XY的 gcd ,比如GCD并找到newX = X / GCDnewY = Y / GCD以删除公共素数因子。
  • 找出频率为newXnewY的质因数。
  • 检查newX 的每个素因数都存在于Y(将除 Y) 中,并且newY 的每个素因数都存在于X(将除 X) 中如果 不是然后返回-1。  
  • 初始化一个变量,比如ans =0,以存储所需的操作数。
  • newXnewY的素数的所有频率添加到ans
  • 返回ans

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to calculate total number of
// prime factor with their prime factor
unordered_map PrimeFactor(int N)
{
    unordered_map primef;
     
    // Iterate while the number is even
    while (N % 2 == 0)
    {
        if (primef.count(2))
        {
            primef[2] += 1;
        }
        else
        {
            primef[2] = 1;
        }
         
        // Reduce to half
        N /= 2;
    }
     
    // Iterate up to sqrt(N)
    for(int i = 3; i <= sqrt(N); i++)
    {
         
        // Iterate while N has
        // factors of i
        while (N % i == 0)
        {
            if (primef.count(i))
            {
                primef[i] += 1;
            }
            else
            {
                primef[i] = 1;
            }
             
            // Removing one factor of i
            N /= 2;
        }
    }
    if (N > 2)
    {
        primef[N] = 1;
    }
    return primef;
}
 
// Function to count the number of factors
int CountToMakeEqual(int X, int Y)
{
     
    // Find the GCD
    int gcdofXY = __gcd(X, Y);
     
    // Find multiples left in X and Y
    int newX = Y / gcdofXY;
    int newY = X / gcdofXY;
     
    // Find prime factor of multiple
    // left in X and Y
    unordered_map primeX;
    unordered_map primeY;
    primeX = PrimeFactor(newX);
    primeY = PrimeFactor(newY);
     
    // Initialize ans
    int ans = 0;
     
    // Check if it possible
    // to obtain X or not
    for(auto c : primeX)
    {
        if (X % c.first != 0)
        {
            return -1;
        }
        ans += primeX[c.first];
    }
     
    // Check if it possible to
    // obtain Y or not
    for(auto c : primeY)
    {
        if (Y % c.first != 0)
        {
            return -1;
        }
        ans += primeY[c.first];
    }
     
    // return main ans
    return ans;
}
 
// Driver code
int main()
{
     
    // Given Input
    int X = 36;
    int Y = 48;
     
    // Function Call
    int ans = CountToMakeEqual(X, Y);
    cout << ans << endl;
    return 0;
}
 
// This code is contributed by adarshsinghk


Java
// Java program for the above approach
import java.util.*;
public class Main
{
     
    static int gcd(int a, int b)
    {
          
        // Everything divides 0
        if (b == 0)
        {
            return a;
        }
        return gcd(b, a % b);
    }
      
    // Function to calculate total number of
    // prime factor with their prime factor
    static HashMap PrimeFactor(int N)
    {
        HashMap primef = new HashMap();
          
        // Iterate while the number is even
        while (N % 2 == 0)
        {
            if (primef.containsKey(2))
            {
                primef.put(2, primef.get(2) + 1);
            }
            else
            {
                primef.put(2, 1);
            }
              
            // Reduce to half
            N = N / 2;
        }
          
        // Iterate up to sqrt(N)
        for(int i = 3; i <= Math.sqrt(N); i++)
        {
          
            // Iterate while N has
            // factors of i
            while (N % i == 0)
            {
                if (primef.containsKey(i))
                {
                    primef.put(i, primef.get(i) + 1);
                }
                else
                {
                    primef.put(i, 1);
                }
                  
                // Removing one factor of i
                N = N / 2;
            }
        }
        if (N > 2)
        {
            primef.put(N, 1);
        }
        return primef;
    }
      
    // Function to count the number of factors
    static int CountToMakeEqual(int X, int Y)
    {
      
        // Find the GCD
        int gcdofXY = gcd(X, Y);
          
        // Find multiples left in X and Y
        int newX = Y / gcdofXY;
        int newY = X / gcdofXY;
          
        // Find prime factor of multiple
        // left in X and Y
        HashMap primeX = PrimeFactor(newX);
        HashMap primeY = PrimeFactor(newY);
          
        // Initialize ans
        int ans = 0;
          
        // Check if it possible
        // to obtain X or not
        for (Map.Entry keys : primeX.entrySet()) {
            if (X % (int)keys.getKey() != 0)
            {
                return -1;
            }
            ans += primeX.get(keys.getKey());
        }
          
        // Check if it possible to
        // obtain Y or not
        for (Map.Entry keys : primeY.entrySet()) {
            if (Y % (int)keys.getKey() != 0)
            {
                return -1;
            }
            ans += primeY.get(keys.getKey());
        }
          
        // Return main ans
        return ans;
    }
 
    public static void main(String[] args) {
        // Given Input
        int X = 36;
        int Y = 48;
          
        // Function Call
        int ans = CountToMakeEqual(X, Y);
        System.out.println(ans);
    }
}
 
// This code is contributed by mukesh07.


Python3
# Python program for the above approach
import math
 
# Function to calculate total number of
# prime factor with their prime factor
 
 
def PrimeFactor(N):
    ANS = dict()
 
    # Iterate while the number is even
    while N % 2 == 0:
        if 2 in ANS:
            ANS[2] += 1
        else:
            ANS[2] = 1
 
        # Reduce to half
        N = N//2
 
    # Iterate up to sqrt(N)
    for i in range(3, int(math.sqrt(N))+1, 2):
 
        # Iterate while N has
        # factors of i
        while N % i == 0:
            if i in ANS:
                ANS[i] += 1
            else:
                ANS[i] = 1
 
            # Removing one factor of i
            N = N // i
 
    if 2 < N:
        ANS[N] = 1
 
    return ANS
 
 
# Function to count the number of factors
def CountToMakeEqual(X, Y):
 
    # Find the GCD
    GCD = math.gcd(X, Y)
 
    # Find multiples left in X and Y
    newY = X//GCD
    newX = Y//GCD
 
    # Find prime factor of multiple
    # left in X and Y
    primeX = PrimeFactor(newX)
    primeY = PrimeFactor(newY)
 
    # Initialize ans
    ans = 0
 
    # Check if it possible
    # to obtain X or not
    for factor in primeX:
 
        if X % factor != 0:
            return -1
        ans += primeX[factor]
 
    # Check if it possible to
    # obtain Y or not
    for factor in primeY:
 
        if Y % factor != 0:
            return -1
        ans += primeY[factor]
 
    # return main ans
    return ans
 
 
# Driver Code
if __name__ == "__main__":
 
    # Given Input
    X = 36
    Y = 48
 
    # Function Call
    ans = CountToMakeEqual(X, Y)
    print(ans)


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
     
static int gcd(int a, int b)
{
     
    // Everything divides 0
    if (b == 0)
    {
        return a;
    }
    return gcd(b, a % b);
}
 
// Function to calculate total number of
// prime factor with their prime factor
static Dictionary PrimeFactor(int N)
{
    Dictionary primef = new Dictionary();
     
    // Iterate while the number is even
    while (N % 2 == 0)
    {
        if (primef.ContainsKey(2))
        {
            primef[2]++;
        }
        else
        {
            primef[2] = 1;
        }
         
        // Reduce to half
        N = N / 2;
    }
     
    // Iterate up to sqrt(N)
    for(int i = 3; i <= Math.Sqrt(N); i++)
    {
     
        // Iterate while N has
        // factors of i
        while (N % i == 0)
        {
            if (primef.ContainsKey(i))
            {
                primef[i]++;
            }
            else
            {
                primef[i] = 1;
            }
             
            // Removing one factor of i
            N = N / 2;
        }
    }
    if (N > 2)
    {
        primef[N] = 1;
    }
    return primef;
}
 
// Function to count the number of factors
static int CountToMakeEqual(int X, int Y)
{
 
    // Find the GCD
    int gcdofXY = gcd(X, Y);
     
    // Find multiples left in X and Y
    int newX = Y / gcdofXY;
    int newY = X / gcdofXY;
     
    // Find prime factor of multiple
    // left in X and Y
    Dictionary primeX = PrimeFactor(newX);
    Dictionary primeY = PrimeFactor(newY);
     
    // Initialize ans
    int ans = 0;
     
    // Check if it possible
    // to obtain X or not
    foreach(KeyValuePair keys in primeX)
    {
        if (X % keys.Key != 0)
        {
            return -1;
        }
        ans += primeX[keys.Key];
    }
     
    // Check if it possible to
    // obtain Y or not
    foreach(KeyValuePair keys in primeY)
    {
        if (Y % keys.Key != 0)
        {
            return -1;
        }
        ans += primeY[keys.Key];
    }
     
    // Return main ans
    return ans;
}
 
// Driver Code
static void Main()
{
     
    // Given Input
    int X = 36;
    int Y = 48;
     
    // Function Call
    int ans = CountToMakeEqual(X, Y);
    Console.Write(ans);
}
}
 
// This code is contributed by rameshtravel07


Javascript


输出:
3

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