Dados tres números enteros N , M y K , la tarea es calcular la suma de los productos de los coeficientes binomiales C(N, i) y C(M, K – i), donde i oscila entre [0, K] .
Ejemplos:
Entrada: N = 2, M = 2, K = 2
Salida: 6
Explicación:
C(2, 0) * C(2, 2) + C(2, 1) * C(2, 1) + C(2, 2) * C(2, 0) = 1*1 + 2*2 +1*1 = 6
Entrada: N = 2, M = 3, K = 1
Salida: 5
Explicación:
C(2, 0) * C( 3, 1) + C(2, 1) * C(3, 0) = 1*3 + 2*1 = 5
Enfoque ingenuo: el enfoque más simple para resolver este problema es simplemente iterar sobre el rango [0, K] y calcular C(N, i) y C(M, K – 1) para cada i y actualizar la suma sumando su producto.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of // the above approach #include <bits/stdc++.h> using namespace std; // Function returns nCr // i.e. Binomial Coefficient int nCr(int n, int r) { // Initialize res with 1 int res = 1; // Since C(n, r) = C(n, n-r) if (r > n - r) r = n - r; // Evaluating expression for (int i = 0; i < r; ++i) { res *= (n - i); res /= (i + 1); } return res; } // Function to calculate and // return the sum of the products int solve(int n, int m, int k) { // Initialize sum to 0 int sum = 0; // Traverse from 0 to k for (int i = 0; i <= k; i++) sum += nCr(n, i) * nCr(m, k - i); return sum; } // Driver Code int main() { int n = 3, m = 2, k = 2; cout << solve(n, m, k); return 0; }
Java
// Java implementation of // the above approach import java.util.*; class GFG{ // Function returns nCr // i.e. Binomial Coefficient static int nCr(int n, int r) { // Initialize res with 1 int res = 1; // Since C(n, r) = C(n, n-r) if (r > n - r) r = n - r; // Evaluating expression for (int i = 0; i < r; ++i) { res *= (n - i); res /= (i + 1); } return res; } // Function to calculate and // return the sum of the products static int solve(int n, int m, int k) { // Initialize sum to 0 int sum = 0; // Traverse from 0 to k for (int i = 0; i <= k; i++) sum += nCr(n, i) * nCr(m, k - i); return sum; } // Driver Code public static void main(String[] args) { int n = 3, m = 2, k = 2; System.out.print(solve(n, m, k)); } } // This code is contributed by Rohit_ranjan
Python3
# Python3 implementation of # the above approach # Function returns nCr # i.e. Binomial Coefficient def nCr(n, r): # Initialize res with 1 res = 1 # Since C(n, r) = C(n, n-r) if r > n - r: r = n - r # Evaluating expression for i in range(r): res *= (n - i) res /= (i + 1) return res; # Function to calculate and # return the sum of the products def solve(n, m, k): # Initialize sum to 0 sum = 0; # Traverse from 0 to k for i in range(k + 1): sum += nCr(n, i) * nCr(m, k - i) return int(sum) # Driver code if __name__ == '__main__': n = 3 m = 2 k = 2; print(solve(n, m, k)) # This code is contributed by jana_sayantan
C#
// C# implementation of // the above approach using System; class GFG{ // Function returns nCr // i.e. Binomial Coefficient static int nCr(int n, int r) { // Initialize res with 1 int res = 1; // Since C(n, r) = C(n, n-r) if (r > n - r) r = n - r; // Evaluating expression for (int i = 0; i < r; ++i) { res *= (n - i); res /= (i + 1); } return res; } // Function to calculate and // return the sum of the products static int solve(int n, int m, int k) { // Initialize sum to 0 int sum = 0; // Traverse from 0 to k for (int i = 0; i <= k; i++) sum += nCr(n, i) * nCr(m, k - i); return sum; } // Driver Code public static void Main(String[] args) { int n = 3, m = 2, k = 2; Console.Write(solve(n, m, k)); } } // This code is contributed by Rajput-Ji
Javascript
<script> // JavaScript program for the above approach // Function returns nCr // i.e. Binomial Coefficient function nCr(n, r) { // Initialize res with 1 let res = 1; // Since C(n, r) = C(n, n-r) if (r > n - r) r = n - r; // Evaluating expression for (let i = 0; i < r; ++i) { res *= (n - i); res /= (i + 1); } return res; } // Function to calculate and // return the sum of the products function solve(n, m, k) { // Initialize sum to 0 let sum = 0; // Traverse from 0 to k for (let i = 0; i <= k; i++) sum += nCr(n, i) * nCr(m, k - i); return sum; } // Driver Code let n = 3, m = 2, k = 2; document.write(solve(n, m, k)); </script>
10
Complejidad temporal: O(K 2 )
Espacio auxiliar: O(1)
Enfoque eficiente:
el enfoque anterior se puede optimizar utilizando la identidad de Vandermonde.
Según la Identidad de Vandermonde , cualquier combinación de K elementos de un total de ( N + M ) elementos debe tener r elementos de M y ( K – r ) elementos de N elementos.
Por lo tanto, la expresión dada se reduce a la siguiente:
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of // the above approach #include <bits/stdc++.h> using namespace std; // Function returns nCr // i.e. Binomial Coefficient int nCr(int n, int r) { // Initialize res with 1 int res = 1; // Since C(n, r) = C(n, n-r) if (r > n - r) r = n - r; // Evaluating expression for (int i = 0; i < r; ++i) { res *= (n - i); res /= (i + 1); } return res; } // Driver Code int main() { int n = 3, m = 2, k = 2; cout << nCr(n + m, k); return 0; }
Java
// Java implementation of // the above approach import java.util.*; class GFG{ // Function returns nCr // i.e. Binomial Coefficient static int nCr(int n, int r) { // Initialize res with 1 int res = 1; // Since C(n, r) = C(n, n-r) if (r > n - r) r = n - r; // Evaluating expression for (int i = 0; i < r; ++i) { res *= (n - i); res /= (i + 1); } return res; } // Driver Code public static void main(String[] args) { int n = 3, m = 2, k = 2; System.out.print(nCr(n + m, k)); } } // This code is contributed by sapnasingh4991
Python3
# Python3 implementation of # the above approach # Function returns nCr # i.e. Binomial Coefficient def nCr(n, r): # Initialize res with 1 res = 1 # Since C(n, r) = C(n, n-r) if(r > n - r): r = n - r # Evaluating expression for i in range(r): res *= (n - i) res //= (i + 1) return res # Driver Code if __name__ == '__main__': n = 3 m = 2 k = 2 # Function call print(nCr(n + m, k)) # This code is contributed by Shivam Singh
C#
// C# implementation of // the above approach using System; class GFG{ // Function returns nCr // i.e. Binomial Coefficient static int nCr(int n, int r) { // Initialize res with 1 int res = 1; // Since C(n, r) = C(n, n-r) if (r > n - r) r = n - r; // Evaluating expression for (int i = 0; i < r; ++i) { res *= (n - i); res /= (i + 1); } return res; } // Driver Code public static void Main() { int n = 3, m = 2, k = 2; Console.Write(nCr(n + m, k)); } } // This code is contributed by Code_Mech
Javascript
<script> // JavaScript implementation of the above approach // Function returns nCr // i.e. Binomial Coefficient function nCr(n, r) { // Initialize res with 1 let res = 1; // Since C(n, r) = C(n, n-r) if (r > n - r) r = n - r; // Evaluating expression for (let i = 0; i < r; ++i) { res *= (n - i); res /= (i + 1); } return res; } // Driver code let n = 3, m = 2, k = 2; document.write(nCr(n + m, k)); // This code is contributed by code_hunt. </script>
10
Complejidad temporal: O(K)
Espacio auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por math_lover y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA