NM ( M > 1)
Ejemplos:
Entrada: N = 12, M = 2
Salida: 2
Explicación: Las potencias de 2 que dividen a 12 son 1 y 2 (2 1 = 2 y 2 2 = 4 que dividen a 12).
La potencia superior es 2, por lo tanto, considere 2.Entrada: N = 500, M = 5
Salida: 3.
Enfoque ingenuo y de manipulación de bits : el enfoque ingenuo y el enfoque de manipulación de bits ya se mencionan en el Conjunto 1 de este problema.
Enfoque eficiente : la tarea se puede resolver utilizando una técnica de búsqueda binaria en el rango [1, log B (A)]. Para cada valor x en el rango, verifique si M x divide a N . Finalmente, devuelve el mayor valor posible.
Siga los pasos a continuación para resolver el problema:
- Encuentre el valor de log M (N)
- Ejecute la búsqueda binaria en el rango [1, log M (N)] .
- Para cada valor x , verifique si M x divide a N y encuentre el mayor valor posible.
A continuación se muestra la implementación del enfoque anterior.
C++
// C++ program to find the Highest // Power of M that divides N #include <bits/stdc++.h> 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
<script> // JavaScript code for the above approach // Function to find any log(N) base M function 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 function HighestPower(N, M) { let start = 0, end = log_a_to_base_b(N, M); let ans = 0; while (start <= end) { let mid = start + Math.floor((end - start) / 2); let temp = (Math.pow(M, mid)); if (N % temp == 0) { ans = mid; start = mid + 1; } else { end = mid - 1; } } return ans; } // Driver code let N = 12; let M = 2; document.write(HighestPower(N, M) + '<br>'); // This code is contributed by Potta Lokesh </script>
2
Complejidad de tiempo : O(log(log M (N)))
Espacio auxiliar : O(1)
Publicación traducida automáticamente
Artículo escrito por arnavjha07 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA