Dados dos enteros N o M, ¿encontrar el número de ceros finales en el producto de factoriales (N!*M!)?
Ejemplos:
Input : N = 4, M = 5 Output : 1 Explanation : 4! = 24, 5! = 120 Product has only 1 trailing 0. Input : N = 127!, M = 57! Output : 44
Como se discutió en número de ceros en N! se puede calcular dividiendo recursivamente N por 5 y sumando los cocientes.
Por ejemplo, si N = 127, entonces
¡Número de 0 en 127! = 127/5 + 127/25 + 127/125 + 127/625
= 25 + 5 + 1 + 0
= 31
Número de ceros en N! = 31. De manera similar, para M podemos calcular y sumar ambos.
Por lo anterior, podemos concluir que el número de ceros en N!*M! ¡Es igual a la suma del número de ceros en N! ¡y M!.
f(N) = piso(N/5) + piso(N/5^2) + … piso(N/5^3) + …
f(M) = piso(x/5) + piso(M/5^ 2) +… suelo(M/5^3) +…
Entonces la respuesta es f(N)+f(M)
C++
// CPP program for count number of trailing zeros // in N! * M! #include <iostream> using namespace std; // Returns number of zeros in factorial n int trailingZero(int x) { // dividing x by powers of 5 and update count int i = 5, count = 0; while (x > i) { count = count + x / i; i = i * 5; } return count; } // Returns count of trailing zeros in M! x N! int countProductTrailing(int M, int N) { return trailingZero(N) + trailingZero(M); } // Driver program int main() { int N = 67, M = 98; cout << countProductTrailing(N, M); return 0; }
Java
// Java program for count number // of trailing zeros in N! * M! import java.io.*; class GFG { // Returns number of zeros in factorial n static int trailingZero(int x) { // dividing x by powers // of 5 and update count int i = 5, count = 0; while (x > i) { count = count + x / i; i = i * 5; } return count; } // Returns count of trailing zeros in M! x N! static int countProductTrailing(int M, int N) { return trailingZero(N) + trailingZero(M); } // Driver program public static void main(String args[]) { int N = 67, M = 98; System.out.println(countProductTrailing(N, M)); } } /* This code is contributed by Nikita Tiwari.*/
Python3
# Python 3 program for count # number of trailing zeros # in N ! * M ! # Returns number of zeros in # factorial n def trailingZero(x) : # Dividing x by powers of 5 # and update count i = 5 count = 0 while (x > i) : count = count + x // i i = i * 5 return count # Returns count of trailing # zeros in M ! x N ! def countProductTrailing(M, N) : return trailingZero(N) + trailingZero(M) # Driver program N = 67 M = 98 print(countProductTrailing(N, M)) # This code is contributed by Nikita Tiwari.
C#
// Java program for count number // of trailing zeros in N! * M! using System; class GFG { // Returns number of zeros in factorial n static int trailingZero(int x) { // dividing x by powers // of 5 and update count int i = 5, count = 0; while (x > i) { count = count + x / i; i = i * 5; } return count; } // Returns count of trailing zeros in M! x N! static int countProductTrailing(int M, int N) { return trailingZero(N) + trailingZero(M); } // Driver program public static void Main() { int N = 67, M = 98; Console.WriteLine(countProductTrailing(N, M)); } } /* This code is contributed by vt_m.*/
PHP
<?php // PHP program for count number // of trailing zeros in N! * M! // Returns number of // zeros in factorial n function trailingZero($x) { // dividing x by powers of // 5 and update count $i = 5; $count = 0; while ($x > $i) { $count = $count + (int)($x / $i); $i = $i * 5; } return $count; } // Returns count of trailing // zeros in M! x N! function countProductTrailing($M, $N) { return trailingZero($N) + trailingZero($M); } // Driver Code $N = 67; $M = 98; echo(countProductTrailing($N, $M)); // This code is contributed by Ajit. ?>
Javascript
<script> // Javascript program for count number // of trailing zeros in N! * M! // Returns number of // zeros in factorial n function trailingZero(x) { // dividing x by powers of // 5 and update count let i = 5; let count = 0; while (x > i) { count = count + parseInt(x / i); i = i * 5; } return count; } // Returns count of trailing // zeros in M! x N! function countProductTrailing(M, N) { return trailingZero(N) + trailingZero(M); } // Driver Code let N = 67; let M = 98; document.write(countProductTrailing(N, M)); // This code is contributed by _saurabh_jaiswal. </script>
Producción:
37
Publicación traducida automáticamente
Artículo escrito por jaingyayak y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA