📜  给定未知产品的最大GCD

📅  最后修改于: 2021-05-04 14:55:15             🧑  作者: Mango

给定两个整数NP ,其中PN个未知整数的乘积,任务是找到这些整数的GCD。可能会有不同的整数组给出相同的乘积,在这种情况下,打印GCD,它在所有可能的组中最大。

例子:

方法:g1 ,a 2 ,a 3 ,…,a n的gcd。因为,每个i的a i必须是g的倍数,并且乘积P = a 1 * a 2 * a 3 *…* a n必须是g n的倍数。答案是使g n %P = 0的最大值g
P = k 1 p 1 * k 2 p 2 * k 3 p 3 *…* k n p t 。那么g的形式必须为k 1 p 1 * k 2 p 2 * k 3 p 3 *…* k n p t 。为了使g最大化,我们必须选择p i = p i / N

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include
using namespace std;
  
// Function to return the required gcd
long max_gcd(long n, long p)
{
    int count = 0;
    long gcd = 1;
  
    // Count the number of times 2 divides p
    while (p % 2 == 0)
    {
  
        // Equivalent to p = p / 2;
        p >>= 1;
        count++;
    }
  
    // If 2 divides p
    if (count > 0)
        gcd *= (long)pow(2, count / n);
  
    // Check all the possible numbers
    // that can divide p
    for (long i = 3; i <= sqrt(p); i += 2) 
    {
        count = 0;
        while (p % i == 0) 
        {
            count++;
            p = p / i;
        }
        if (count > 0)
        {
            gcd *= (long)pow(i, count / n);
        }
    }
  
    // If n in the end is a prime number
    if (p > 2)
        gcd *= (long)pow(p, 1 / n);
  
    // Return the required gcd
    return gcd;
}
  
// Driver code
int main()
{
    long n = 3;
    long p = 80;
    cout << max_gcd(n, p);
}
      
// This code is contributed by Code_Mech


Java
// Java implementation of the approach
class GFG {
  
    // Function to return the required gcd
    static long max_gcd(long n, long p)
    {
        int count = 0;
        long gcd = 1;
  
        // Count the number of times 2 divides p
        while (p % 2 == 0) {
  
            // Equivalent to p = p / 2;
            p >>= 1;
            count++;
        }
  
        // If 2 divides p
        if (count > 0)
            gcd *= (long)Math.pow(2, count / n);
  
        // Check all the possible numbers
        // that can divide p
        for (long i = 3; i <= Math.sqrt(p); i += 2) {
            count = 0;
            while (p % i == 0) {
                count++;
                p = p / i;
            }
            if (count > 0) {
                gcd *= (long)Math.pow(i, count / n);
            }
        }
  
        // If n in the end is a prime number
        if (p > 2)
            gcd *= (long)Math.pow(p, 1 / n);
  
        // Return the required gcd
        return gcd;
    }
  
    // Driver code
    public static void main(String[] args)
    {
        long n = 3;
        long p = 80;
        System.out.println(max_gcd(n, p));
    }
}


Python3
# Python3 implementation of the approach
import math
  
# Function to return the required gcd 
def max_gcd(n, p): 
  
    count = 0; 
    gcd = 1; 
  
    # Count the number of times 2 divides p 
    while (p % 2 == 0): 
      
        # Equivalent to p = p / 2; 
        p >>= 1; 
        count = count + 1; 
      
    # If 2 divides p 
    if (count > 0): 
        gcd = gcd * pow(2, count // n); 
  
    # Check all the possible numbers 
    # that can divide p 
    for i in range(3, (int)(math.sqrt(p)), 2): 
      
        count = 0; 
        while (p % i == 0): 
          
            count = count + 1; 
            p = p // i; 
          
        if (count > 0):
          
            gcd = gcd * pow(i, count // n); 
          
    # If n in the end is a prime number 
    if (p > 2) : 
        gcd = gcd * pow(p, 1 // n); 
  
    # Return the required gcd 
    return gcd; 
  
# Driver code 
n = 3; 
p = 80; 
print(max_gcd(n, p)); 
  
# This code is contributed by Shivi_Aggarwal


C#
// C# implementation of the approach
using System;
  
class GFG 
{ 
  
// Function to return the required gcd 
static long max_gcd(long n, long p) 
{ 
    int count = 0; 
    long gcd = 1; 
  
    // Count the number of times 2 divides p 
    while (p % 2 == 0) 
    { 
  
        // Equivalent to p = p / 2; 
        p >>= 1; 
        count++; 
    } 
  
    // If 2 divides p 
    if (count > 0) 
        gcd *= (long)Math.Pow(2, count / n); 
  
    // Check all the possible numbers 
    // that can divide p 
    for (long i = 3; i <= Math.Sqrt(p); i += 2) 
    { 
        count = 0; 
        while (p % i == 0) 
        { 
            count++; 
            p = p / i; 
        } 
        if (count > 0)
        { 
            gcd *= (long)Math.Pow(i, count / n); 
        } 
    } 
  
    // If n in the end is a prime number 
    if (p > 2) 
        gcd *= (long)Math.Pow(p, 1 / n); 
  
    // Return the required gcd 
    return gcd; 
} 
  
// Driver code 
public static void Main() 
{ 
    long n = 3; 
    long p = 80; 
    Console.WriteLine(max_gcd(n, p)); 
} 
} 
  
// This code is contributed by Ryuga


PHP
>= 1;
        $count++;
    }
  
    // If 2 divides p
    if ($count > 0)
        $gcd *= pow(2, (int)($count / $n));
  
    // Check all the possible numbers
    // that can divide p
    for ($i = 3; $i <= (int)sqrt($p); $i += 2) 
    {
        $count = 0;
        while ($p % $i == 0) 
        {
            $count++;
            $p = (int)($p / $i);
        }
        if ($count > 0) 
        {
            $gcd *= pow($i, (int)($count / $n));
        }
    }
  
    // If n in the end is a prime number
    if ($p > 2)
        $gcd *= pow($p, (int)(1 / $n));
  
    // Return the required gcd
    return $gcd;
}
  
// Driver code
$n = 3;
$p = 80;
echo(max_gcd($n, $p));
  
// This code is contributed by Code_Mech


输出:
2