Dados cuatro enteros X , Y , M y W . La tarea es encontrar el número de formas de elegir X hombres y Y mujeres del total de M hombres y W mujeres.
Ejemplos:
Entrada: X = 1, Y = 2, M = 1, W = 3
Salida: 3
Vía 1: Elige el único hombre y 1º y 2º mujer .
Vía 2: Elige el único hombre y 2º y 3º mujer.
Vía 3: Elige el único hombre y 1ª y 3ª mujer.Entrada: X = 4, Y = 3, M = 6, W = 5
Salida: 150
Enfoque: El número total de formas de elegir X hombres de un total de M hombres es M C X y el número total de formas de elegir Y mujeres de W mujeres es W C Y . Por lo tanto, el número total de vías combinadas será M C X * W C Y .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the // value of ncr effectively int ncr(int n, int r) { // Initialize the answer int ans = 1; for (int i = 1; i <= r; i += 1) { // Divide simultaneously by // i to avoid overflow ans *= (n - r + i); ans /= i; } return ans; } // Function to return the count of required ways int totalWays(int X, int Y, int M, int W) { return (ncr(M, X) * ncr(W, Y)); } int main() { int X = 4, Y = 3, M = 6, W = 5; cout << totalWays(X, Y, M, W); return 0; }
Java
// JAVA implementation of the approach import java.io.*; class GFG { // Function to return the // value of ncr effectively static int ncr(int n, int r) { // Initialize the answer int ans = 1; for (int i = 1; i <= r; i += 1) { // Divide simultaneously by // i to avoid overflow ans *= (n - r + i); ans /= i; } return ans; } // Function to return the count of required ways static int totalWays(int X, int Y, int M, int W) { return (ncr(M, X) * ncr(W, Y)); } // Driver code public static void main (String[] args) { int X = 4, Y = 3, M = 6, W = 5; System.out.println(totalWays(X, Y, M, W)); } } // This code is contributed by ajit_23
Python3
# Python3 implementation of the approach # Function to return the # value of ncr effectively def ncr(n, r): # Initialize the answer ans = 1 for i in range(1,r+1): # Divide simultaneously by # i to avoid overflow ans *= (n - r + i) ans //= i return ans # Function to return the count of required ways def totalWays(X, Y, M, W): return (ncr(M, X) * ncr(W, Y)) X = 4 Y = 3 M = 6 W = 5 print(totalWays(X, Y, M, W)) # This code is contributed by mohit kumar 29
C#
// C# implementation of the approach using System; class GFG { // Function to return the // value of ncr effectively static int ncr(int n, int r) { // Initialize the answer int ans = 1; for (int i = 1; i <= r; i += 1) { // Divide simultaneously by // i to avoid overflow ans *= (n - r + i); ans /= i; } return ans; } // Function to return the count of required ways static int totalWays(int X, int Y, int M, int W) { return (ncr(M, X) * ncr(W, Y)); } // Driver code static public void Main () { int X = 4, Y = 3, M = 6, W = 5; Console.WriteLine(totalWays(X, Y, M, W)); } } // This code is contributed by AnkitRai01
Javascript
<script> // Javascript implementation of the approach // Function to return the // value of ncr effectively function ncr(n, r) { // Initialize the answer let ans = 1; for (let i = 1; i <= r; i += 1) { // Divide simultaneously by // i to avoid overflow ans *= (n - r + i); ans = parseInt(ans / i); } return ans; } // Function to return the count of required ways function totalWays(X, Y, M, W) { return (ncr(M, X) * ncr(W, Y)); } // Driver Code let X = 4, Y = 3, M = 6, W = 5; document.write(totalWays(X, Y, M, W)); // This code is contributed by rishavmahato348. </script>
Producción:
150
Complejidad de tiempo: O(n)
Espacio Auxiliar: O(1)