📌  相关文章
📜  一个数字的最高幂除以其他数字

📅  最后修改于: 2021-04-22 06:17:37             🧑  作者: Mango

给定两个数字NM ,任务是找到除以N的M的最高幂。

注意:M> 1

例子:

方法:最初对NM进行素数分解,并将素数的计数分别存储在NM的freq1 []freq2 []中。对于M的每个素数,检查其freq2 [num]是否大于freq1是否为[num] 。如果它是M的任何素数,则最大功率将为0 。否则,对于每个M的素数因子,最大功率将是所有freq1#/ freq2 ##中的最小值

对于数字N = 24,素数因子将为2 ^ 3 * 3 ^ 1 。因此,freq1 [2] = 3,freq1 [3] = 1。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
  
// Function to get the prime factors
// and its count of times it divides
void primeFactors(int n, int freq[])
{
  
    int cnt = 0;
  
    // Count the number of 2s that divide n
    while (n % 2 == 0) {
        cnt++;
        n = n / 2;
    }
  
    freq[2] = cnt;
  
    // n must be odd at this point. So we can skip
    // one element (Note i = i +2)
    for (int i = 3; i <= sqrt(n); i = i + 2) {
        cnt = 0;
  
        // While i divides n, count i and divide n
        while (n % i == 0) {
            cnt++;
            n = n / i;
        }
  
        freq[i] = cnt;
    }
  
    // This condition is to handle the case when n
    // is a prime number greater than 2
    if (n > 2)
        freq[n] = 1;
}
  
// Function to return the highest power
int getMaximumPower(int n, int m)
{
  
    // Initialize two arrays
    int freq1[n + 1], freq2[m + 1];
  
    memset(freq1, 0, sizeof freq1);
    memset(freq2, 0, sizeof freq2);
  
    // Get the prime factors of n and m
    primeFactors(n, freq1);
    primeFactors(m, freq2);
  
    int maxi = 0;
  
    // Iterate and find the maximum power
    for (int i = 2; i <= m; i++) {
  
        // If i not a prime factor of n and m
        if (freq1[i] == 0 && freq2[i] == 0)
            continue;
  
        // If i is a prime factor of n and m
        // If count of i dividing m is more
        // than i dividing n, then power will be 0
        if (freq2[i] > freq1[i])
            return 0;
  
        // If i is a prime factor of M
        if (freq2[i]) {
  
            // get the maximum power
            maxi = max(maxi, freq1[i] / freq2[i]);
        }
    }
  
    return maxi;
}
  
// Drivers code
int main()
{
    int n = 48, m = 4;
    cout << getMaximumPower(n, m);
    return 0;
}


Java
// Java program to implement
// the above approach
  
class GFG 
{
  
// Function to get the prime factors
// and its count of times it divides
static void primeFactors(int n, int freq[])
{
  
    int cnt = 0;
  
    // Count the number of 2s that divide n
    while (n % 2 == 0) 
    {
        cnt++;
        n = n / 2;
    }
  
    freq[2] = cnt;
  
    // n must be odd at this point. So we can skip
    // one element (Note i = i +2)
    for (int i = 3; i <= Math.sqrt(n); i = i + 2)
    {
        cnt = 0;
  
        // While i divides n, count i and divide n
        while (n % i == 0)
        {
            cnt++;
            n = n / i;
        }
  
        freq[i] = cnt;
    }
  
    // This condition is to handle the case when n
    // is a prime number greater than 2
    if (n > 2)
        freq[n] = 1;
}
  
// Function to return the highest power
static int getMaximumPower(int n, int m)
{
  
    // Initialize two arrays
    int freq1[] = new int[n + 1], freq2[] = new int[m + 1];
  
    // Get the prime factors of n and m
    primeFactors(n, freq1);
    primeFactors(m, freq2);
  
    int maxi = 0;
  
    // Iterate and find the maximum power
    for (int i = 2; i <= m; i++)
    {
  
        // If i not a prime factor of n and m
        if (freq1[i] == 0 && freq2[i] == 0)
            continue;
  
        // If i is a prime factor of n and m
        // If count of i dividing m is more
        // than i dividing n, then power will be 0
        if (freq2[i] > freq1[i])
            return 0;
  
        // If i is a prime factor of M
        if (freq2[i] != 0) 
        {
  
            // get the maximum power
            maxi = Math.max(maxi, freq1[i] / freq2[i]);
        }
    }
  
    return maxi;
}
  
// Drivers code
public static void main(String[] args) 
{
    int n = 48, m = 4;
    System.out.println(getMaximumPower(n, m));
  
}
}
  
// This code contributed by Rajput-Ji


Python 3
import math
  
# Python program to implement
# the above approach
  
# Function to get the prime factors
# and its count of times it divides
def primeFactors(n, freq):
    cnt = 0
  
    # Count the number of 2s that divide n
    while n % 2 == 0:
        cnt = cnt + 1
        n = int(n // 2)
  
    freq[2] = cnt
  
    # n must be odd at this point. So we can skip
    # one element (Note i = i+2)
    i=3
    while i<=math.sqrt(n):
        cnt = 0
  
        # While i divides n, count i and divide n
        while (n % i == 0):
            cnt = cnt+1
            n = int(n // i)
              
        freq[int(i)] = cnt
        i=i + 2
          
    # This condition is to handle the case when n
    # is a prime number greater than 2
    if (n > 2):
        freq[int(n)] = 1
  
  
# Function to return the highest power
def getMaximumPower(n, m):
  
    # Initialize two arrays
    freq1 = [0] * (n + 1)
    freq2 = [0] * (m + 1)
  
  
    # Get the prime factors of n and m
    primeFactors(n, freq1)
    primeFactors(m, freq2)
  
    maxi = 0
  
    # Iterate and find the maximum power
    i = 2
    while i <= m:
  
        # If i not a prime factor of n and m
        if (freq1[i] == 0 and freq2[i] == 0):
            i = i + 1
            continue
  
        # If i is a prime factor of n and m
        # If count of i dividing m is more
        # than i dividing n, then power will be 0
        if (freq2[i] > freq1[i]):
            return 0
  
        # If i is a prime factor of M
        if (freq2[i]):
  
            # get the maximum power
            maxi = max(maxi, int(freq1[i] // freq2[i]))
          
        i = i + 1
      
  
    return maxi
  
  
# Drivers code
n = 48
m = 4
print(getMaximumPower(n, m))
  
# This code is contributed by Shashank_Sharma


C#
// C# program to implement
// the above approach
using System;
  
class GFG 
{
  
// Function to get the prime factors
// and its count of times it divides
static void primeFactors(int n, int []freq)
{
  
    int cnt = 0;
  
    // Count the number of 2s that divide n
    while (n % 2 == 0) 
    {
        cnt++;
        n = n / 2;
    }
  
    freq[2] = cnt;
  
    // n must be odd at this point. So we can skip
    // one element (Note i = i +2)
    for (int i = 3; i <= Math.Sqrt(n); i = i + 2)
    {
        cnt = 0;
  
        // While i divides n, count i and divide n
        while (n % i == 0)
        {
            cnt++;
            n = n / i;
        }
  
        freq[i] = cnt;
    }
  
    // This condition is to handle the case when n
    // is a prime number greater than 2
    if (n > 2)
        freq[n] = 1;
}
  
// Function to return the highest power
static int getMaximumPower(int n, int m)
{
  
    // Initialize two arrays
    int []freq1 = new int[n + 1];int []freq2 = new int[m + 1];
  
    // Get the prime factors of n and m
    primeFactors(n, freq1);
    primeFactors(m, freq2);
  
    int maxi = 0;
  
    // Iterate and find the maximum power
    for (int i = 2; i <= m; i++)
    {
  
        // If i not a prime factor of n and m
        if (freq1[i] == 0 && freq2[i] == 0)
            continue;
  
        // If i is a prime factor of n and m
        // If count of i dividing m is more
        // than i dividing n, then power will be 0
        if (freq2[i] > freq1[i])
            return 0;
  
        // If i is a prime factor of M
        if (freq2[i] != 0) 
        {
  
            // get the maximum power
            maxi = Math.Max(maxi, freq1[i] / freq2[i]);
        }
    }
  
    return maxi;
}
  
// Drivers code
public static void Main(String[] args) 
{
    int n = 48, m = 4;
    Console.WriteLine(getMaximumPower(n, m));
  
}
}
  
// This code has been contributed by 29AjayKumar


PHP
 2) 
        $freq[$n] = 1; 
      
    return $freq ;
} 
  
// Function to return the highest power 
function getMaximumPower($n, $m) 
{ 
  
    $freq1 = array_fill(0,$n + 1,0); 
    $freq2 = array_fill(0,$m + 1,0); 
  
    // Get the prime factors of n and m 
    $freq1 = primeFactors($n, $freq1); 
    $freq2 = primeFactors($m, $freq2); 
  
    $maxi = 0; 
  
    // Iterate and find the maximum power 
    for ($i = 2; $i <= $m; $i++) 
    { 
  
        // If i not a prime factor of n and m 
        if ($freq1[$i] == 0 && $freq2[$i] == 0) 
            continue; 
  
        // If i is a prime factor of n and m 
        // If count of i dividing m is more 
        // than i dividing n, then power will be 0 
        if ($freq2[$i] > $freq1[$i]) 
            return 0; 
  
        // If i is a prime factor of M 
        if ($freq2[$i]) 
        { 
  
            // get the maximum power 
            $maxi = max($maxi, floor($freq1[$i] / $freq2[$i])); 
        } 
    } 
  
    return $maxi; 
} 
  
    // Drivers code 
    $n = 48; $m = 4; 
    echo getMaximumPower($n, $m); 
  
    // This code is contributed by Ryuga
?>


输出:
2