Dados dos enteros A y B , la tarea es calcular 2 2A % B .
Ejemplos:
Entrada: A = 3, B = 5
Salida: 1
2 23 % 5 = 2 8 % 5 = 256 % 5 = 1.Entrada: A = 10, B = 13
Salida: 3
Enfoque: el problema se puede resolver de manera eficiente al dividirlo en subproblemas sin desbordamiento de enteros mediante el uso de la recursividad.
Sea F(A, B) = 2 2A % B .
Ahora, F(A, B) = 2 2A % B
= 2 2 * 2A – 1 % B
= (2 2A – 1 + 2A – 1 ) % B
= (2 2A – 1 * 2 2A – 1 ) % B
= (F(A – 1, B) * F(A – 1, B)) % B
Por lo tanto, F(A, B) = (F(A – 1, B) * F(A – 1, B)) % B. _
El caso base es F(1, B) = 2 21 % B = 4 % B.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; #define ll long long // Function to return 2^(2^A) % B ll F(ll A, ll B) { // Base case, 2^(2^1) % B = 4 % B if (A == 1) return (4 % B); else { ll temp = F(A - 1, B); return (temp * temp) % B; } } // Driver code int main() { ll A = 25, B = 50; // Print 2^(2^A) % B cout << F(A, B); return 0; }
Java
// Java implementation of the above approach class GFG { // Function to return 2^(2^A) % B static long F(long A, long B) { // Base case, 2^(2^1) % B = 4 % B if (A == 1) return (4 % B); else { long temp = F(A - 1, B); return (temp * temp) % B; } } // Driver code public static void main(String args[]) { long A = 25, B = 50; // Print 2^(2^A) % B System.out.println(F(A, B)); } } // This code is contributed by Ryuga
Python3
# Python3 implementation of the approach # Function to return 2^(2^A) % B def F(A, B): # Base case, 2^(2^1) % B = 4 % B if (A == 1): return (4 % B); else: temp = F(A - 1, B); return (temp * temp) % B; # Driver code A = 25; B = 50; # Print 2^(2^A) % B print(F(A, B)); # This code is contributed by mits
C#
// C# implementation of the approach class GFG { // Function to return 2^(2^A) % B static long F(long A, long B) { // Base case, 2^(2^1) % B = 4 % B if (A == 1) return (4 % B); else { long temp = F(A - 1, B); return (temp * temp) % B; } } // Driver code static void Main() { long A = 25, B = 50; // Print 2^(2^A) % B System.Console.WriteLine(F(A, B)); } } // This code is contributed by mits
PHP
<?php // PHP implementation of the approach // Function to return 2^(2^A) % B function F($A, $B) { // $Base case, 2^(2^1) % B = 4 % B if ($A == 1) return (4 % $B); else { $temp = F($A - 1, $B); return ($temp * $temp) % $B; } } // Driver code $A = 25; $B = 50; // Print 2^(2^$A) % $B echo F($A, $B); // This code contributed by Rajput-Ji
Javascript
<script> // Javascript implementation of the approach // Function to return 2^(2^A) % B function F(A, B) { // Base case, 2^(2^1) % B = 4 % B if (A == 1) return (4 % B); else { var temp = F(A - 1, B); return (temp * temp) % B; } } // Driver code var A = 25, B = 50; // Print 2^(2^A) % B document.write( F(A, B)); // This code is contributed by noob2000 </script>
46
Publicación traducida automáticamente
Artículo escrito por rupesh_rao y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA