📌  相关文章
📜  通过将X与给定的互质数相乘将X转换为Y所需的最小操作

📅  最后修改于: 2021-04-26 09:16:25             🧑  作者: Mango

给定四个整数X,Y,PQ,使得X≤YGCD(P,Q)= 1。任务是找到将X转换为Y所需的最小操作。在一次操作中,您可以将X乘以PQ。如果无法将X转换为Y,则打印-1
例子:

方法:如果Y不能被X整除,则任何整数与X的任何次数的整数乘积都不会导致Y ,结果为-1
否则,令d = Y / X。现在,必须具有P a * Q b = d才能具有有效的解,并且在这种情况下,如果d不能用PQ的幂表示,则结果将是(a + b)否则为-1
为了检查条件,将d除以PQ,直至可除。现在,如果最终d = 1 ,那么解决方案是可能的,否则就不可能。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the minimum
// operations required
int minOperations(int x, int y, int p, int q)
{
 
    // Not possible
    if (y % x != 0)
        return -1;
 
    int d = y / x;
 
    // To store the greatest power
    // of p that divides d
    int a = 0;
 
    // While divible by p
    while (d % p == 0) {
        d /= p;
        a++;
    }
 
    // To store the greatest power
    // of q that divides d
    int b = 0;
 
    // While divible by q
    while (d % q == 0) {
        d /= q;
        b++;
    }
 
    // If d > 1
    if (d != 1)
        return -1;
 
    // Since, d = p^a * q^b
    return (a + b);
}
 
// Driver code
int main()
{
    int x = 12, y = 2592, p = 2, q = 3;
 
    cout << minOperations(x, y, p, q);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
     
    // Function to return the minimum
    // operations required
    static int minOperations(int x, int y, int p, int q)
    {
     
        // Not possible
        if (y % x != 0)
            return -1;
     
        int d = y / x;
     
        // To store the greatest power
        // of p that divides d
        int a = 0;
     
        // While divible by p
        while (d % p == 0)
        {
            d /= p;
            a++;
        }
     
        // To store the greatest power
        // of q that divides d
        int b = 0;
     
        // While divible by q
        while (d % q == 0)
        {
            d /= q;
            b++;
        }
     
        // If d > 1
        if (d != 1)
            return -1;
     
        // Since, d = p^a * q^b
        return (a + b);
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int x = 12, y = 2592, p = 2, q = 3;
        System.out.println(minOperations(x, y, p, q));
    }
}
 
// This code is contributed by AnkitRai01


Python3
# Python3 implementation of the approach
 
# Function to return the minimum
# operations required
def minOperations(x, y, p, q):
 
    # Not possible
    if (y % x != 0):
        return -1
 
    d = y // x
 
    # To store the greatest power
    # of p that divides d
    a = 0
 
    # While divible by p
    while (d % p == 0):
        d //= p
        a+=1
 
    # To store the greatest power
    # of q that divides d
    b = 0
 
    # While divible by q
    while (d % q == 0):
        d //= q
        b+=1
 
    # If d > 1
    if (d != 1):
        return -1
 
    # Since, d = p^a * q^b
    return (a + b)
 
 
# Driver code
 
x = 12
y = 2592
p = 2
q = 3
 
print(minOperations(x, y, p, q))
 
# This code is contributed by mohit kumar 29


C#
// C# implementation of the approach
using System;
 
class GFG
{
     
    // Function to return the minimum
    // operations required
    static int minOperations(int x, int y, int p, int q)
    {
     
        // Not possible
        if (y % x != 0)
            return -1;
     
        int d = y / x;
     
        // To store the greatest power
        // of p that divides d
        int a = 0;
     
        // While divible by p
        while (d % p == 0)
        {
            d /= p;
            a++;
        }
     
        // To store the greatest power
        // of q that divides d
        int b = 0;
     
        // While divible by q
        while (d % q == 0)
        {
            d /= q;
            b++;
        }
     
        // If d > 1
        if (d != 1)
            return -1;
     
        // Since, d = p^a * q^b
        return (a + b);
    }
     
    // Driver code
    public static void Main ()
    {
        int x = 12, y = 2592, p = 2, q = 3;
        Console.Write(minOperations(x, y, p, q));
    }
}
 
// This code is contributed by anuj_67..


Javascript


输出:
6