📜  一个数除以其他数的最高幂 |套装 – 2

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

一个数除以其他数的最高幂 |套装 – 2

给定两个数NM (M > 1) ,任务是找到除 N 的 M 的最大幂。

例子:

朴素和位操作方法:朴素方法和位操作方法已在本题的第1组中提到。

有效方法:可以使用范围[1, log B (A)] 的二分搜索技术来解决该任务。对于范围内的每个值x ,检查M x是否除以N 。最后,返回可能的最大值

请按照以下步骤解决问题:

  • log M (N)的值
  • [1, log M (N)]范围内运行二进制搜索
  • 对于每个值x ,检查M x是否除以N ,并找到可能的最大值。

下面是上述方法的实现。

C++
// C++ program to find the Highest
// Power of M that divides N
#include 
using namespace std;
 
// Function to find any log(N) base M
int log_a_to_base_b(int a, int b)
{
    return log(a) / log(b);
}
 
// Function to find the Highest Power
// of M which divides N
int HighestPower(int N, int M)
{
    int start = 0, end = log_a_to_base_b(N, M);
    int ans = 0;
    while (start <= end) {
 
        int mid = start + (end - start) / 2;
        int temp = (int)(pow(M, mid));
 
        if (N % temp == 0) {
            ans = mid;
            start = mid + 1;
        }
        else {
            end = mid - 1;
        }
    }
    return ans;
}
 
// Driver code
int main()
{
    int N = 12;
    int M = 2;
    cout << HighestPower(N, M) << endl;
    return 0;
}


Java
// Java program to find the Highest
// Power of M that divides N
import java.util.*;
public class GFG
{
 
  // Function to find any log(N) base M
  static int log_a_to_base_b(int a, int b)
  {
    return (int)(Math.log(a) / Math.log(b));
  }
 
  // Function to find the Highest Power
  // of M which divides N
  static int HighestPower(int N, int M)
  {
    int start = 0, end = log_a_to_base_b(N, M);
    int ans = 0;
    while (start <= end) {
 
      int mid = start + (end - start) / 2;
      int temp = (int)(Math.pow(M, mid));
 
      if (N % temp == 0) {
        ans = mid;
        start = mid + 1;
      }
      else {
        end = mid - 1;
      }
    }
    return ans;
  }
 
  // Driver code
  public static void main(String args[])
  {
    int N = 12;
    int M = 2;
    System.out.println(HighestPower(N, M));
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Python
# Python program to find the Highest
# Power of M that divides N
import math
 
# Function to find any log(N) base M
def log_a_to_base_b(a, b):
     
    return math.log(a) / math.log(b)
 
# Function to find the Highest Power
# of M which divides N
def HighestPower(N, M):
     
    start = 0
    end = log_a_to_base_b(N, M)
    ans = 0
    while (start <= end):
 
        mid = math.floor(start + (end - start) / 2)
        temp = math.pow(M, mid)
 
        if (N % temp == 0):
            ans = mid
            start = mid + 1
             
        else:
            end = mid - 1
 
    return ans
 
# Driver code
N = 12
M = 2
print(HighestPower(N, M))
 
# This code is contributed by Samim Hossain Mondal.


C#
// C# program for the above approach
using System;
class GFG
{
  // Function to find any log(N) base M
  static int log_a_to_base_b(int a, int b)
  {
    return (int)(Math.Log(a) / Math.Log(b));
  }
 
  // Function to find the Highest Power
  // of M which divides N
  static int HighestPower(int N, int M)
  {
    int start = 0, end = log_a_to_base_b(N, M);
    int ans = 0;
    while (start <= end) {
 
      int mid = start + (end - start) / 2;
      int temp = (int)(Math.Pow(M, mid));
 
      if (N % temp == 0) {
        ans = mid;
        start = mid + 1;
      }
      else {
        end = mid - 1;
      }
    }
    return ans;
  }
 
  // Driver code
  public static void Main()
  {
 
    int N = 12;
    int M = 2;
     
    // Function call
    Console.Write(HighestPower(N, M));
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
2

时间复杂度:O(log(log M (N)))
辅助空间:O(1)